#include#include class MyClass : public QObject { Q_OBJECT public: void doSomething() { // create an event loop QEventLoop loop; // connect to a signal connect(this, &MyClass::someSignal, &loop, &QEventLoop::quit); // wait until the signal is emitted loop.exec(); // continue with the rest of the code // ... } signals: void someSignal(); };
#includeIn this example, an event loop is used to process user input events in a console application. The loop waits until the application's `aboutToQuit` and `lastWindowClosed` events are emitted before exiting. The `QObject::connect` calls connect the loop to the application's events. This example uses the QCoreApplication package library.#include #include int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); // create an event loop QEventLoop loop; // connect the loop to the application's events QObject::connect(&app, &QCoreApplication::aboutToQuit, &loop, &QEventLoop::quit); QObject::connect(&app, &QCoreApplication::lastWindowClosed, &loop, &QEventLoop::quit); // wait until the application quits loop.exec(); return 0; }