#include#include #include int main(int argc, char *argv[]) { QApplication app(argc, argv); QTimer::singleShot(5000, [&](){ QMessageBox::information(nullptr, "Timer", "5 seconds have elapsed"); }); return app.exec(); }
#includeIn this example, a QTimer is set to fire 2 seconds after a button click, and show a message box. In the lambda function connected to the button's `clicked` signal, another lambda is used with the `singleShot` method to set the timer to fire after 2 seconds. Package library: Qt#include #include #include int main(int argc, char *argv[]) { QApplication app(argc, argv); QPushButton button("Click me"); QObject::connect(&button, &QPushButton::clicked, [&](){ QTimer::singleShot(2000, [&](){ QMessageBox::information(nullptr, "Timer", "2 seconds have elapsed since button click"); }); }); button.show(); return app.exec(); }