Is there notification when event loop starts?
I have a Qt application with this kind of main()…
- int main(int argc, char *argv[])
- {
- MainWindow mainWin;
- ... A separate, non-GUI thread is launched here
- mainWin.Init();
- mainWin.show();
- app.exec();
- }
This other thread that is created before the mainWin needs to know when it can start communicating with the mainWin. But since the mainWin uses Qt signals, slots, timers, etc, it’s not truly ready to rock until the event loop is running (via exec()).
My question is: is there some signal or event that is emitted when the event loop has started?
Consider this. In mainWin.Init(), you can create something like a QTimer and even call .start() to kick it off. But it won’t actually be run and trigger events until exec() has been called. This is why I need to know when the event loop has truly started.
8 replies
You can easily create such notification with QMetaObject::invokeMethod() called with Qt::QueuedConnection connection type. Something like this:
- // separate thread is launched here
- return app.exec();
notifier->notifyThread() will be queued for execution and will be executed when main event loop starts.
You can easily create such notification with QMetaObject::invokeMethod() called with Qt::QueuedConnection connection type. Something like this:
// separate thread is launched here return app.exec();notifier->notifyThread() will be queued for execution and will be executed when main event loop starts.
This would not work, as it would be executed when the thread event loop runs, not the main one.
A common solution is to use a single shot timer:
int main(int argc, const char* argv[]) { MyThread thread; // .... a.exec(); }
Take care, where the slot is executed. This one will be executed in the main thread if thread is not moved to itself. If it is moved, the same as the upper comment comes into the game, the thread event loop would be taken, the gui event loop might not have started.
You must log in to post a reply. Not a member yet? Register here!



