January 16, 2012

PedroCaliente PedroCaliente
Lab Rat
6 posts

Is there notification when event loop starts?

 

I have a Qt application with this kind of main()…

  1. int main(int argc, char *argv[])
  2.     {
  3.         QApplication app(argc, argv);
  4.         MainWindow   mainWin;
  5.  
  6.         ... A separate, non-GUI thread is launched here
  7.  
  8.         mainWin.Init();
  9.         mainWin.show();
  10.  
  11.         app.exec();
  12.     }

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

January 16, 2012

Andre Andre
Robot Herder
6417 posts

No, there is no real notification mechanism. What you could do, is create a custom event that you post. As soon as that event arrives, you can be sure that your eventloop has started.

January 16, 2012

p-himik p-himik
Lab Rat
263 posts

You can easily create such notification with QMetaObject::invokeMethod() called with Qt::QueuedConnection connection type. Something like this:

  1. // separate thread is launched here
  2. QMetaObject::invokeMethod( notifier, SLOT(notifyThread()), Qt::QueuedConnection );
  3. return app.exec();

notifier->notifyThread() will be queued for execution and will be executed when main event loop starts.

January 16, 2012

Volker Volker
Ant Farmer
5428 posts

A common solution is to use a single shot timer:

  1. int main(int argc, const char* argv[])
  2. {
  3.     QApplication a(argc, argv);
  4.     MyThread thread;
  5.     QTimer::singleShot(0, &thread, SLOT(startYourWork()));
  6.  
  7.     // ....
  8.  
  9.     a.exec();
  10. }

January 16, 2012

Gerolf Gerolf
Robot Herder
3287 posts

p-himik wrote:
You can easily create such notification with QMetaObject::invokeMethod() called with Qt::QueuedConnection connection type. Something like this:
  1. // separate thread is launched here
  2. QMetaObject::invokeMethod( notifier, SLOT(notifyThread()), Qt::QueuedConnection );
  3. 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.

Volker wrote:
A common solution is to use a single shot timer:
  1. int main(int argc, const char* argv[])
  2. {
  3.     QApplication a(argc, argv);
  4.     MyThread thread;
  5.     QTimer::singleShot(0, &thread, SLOT(startYourWork()));
  6.  
  7.     // ....
  8.  
  9.     a.exec();
  10. }

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.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

January 16, 2012

Volker Volker
Ant Farmer
5428 posts
Gerolf wrote:
Take care, where the slot is executed. This one will be executed in the main thread if thread is not moved to itself.

You’re doing it wrong™ :-)

January 16, 2012

p-himik p-himik
Lab Rat
263 posts
Gerolf wrote:
This would not work, as it would be executed when the thread event loop runs, not the main one.

Sorry I didn’t write it but notifier is a separate object created in main() which only purpose is to notify separate thread.

January 16, 2012

Gerolf Gerolf
Robot Herder
3287 posts
Volker wrote:
Gerolf wrote:
Take care, where the slot is executed. This one will be executed in the main thread if thread is not moved to itself.

You’re doing it wrong™ :-)

I did not say do it, I just mentioned the possible problems :-)

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

January 16, 2012

Volker Volker
Ant Farmer
5428 posts
Gerolf wrote:
Volker wrote:
Gerolf wrote:
Take care, where the slot is executed. This one will be executed in the main thread if thread is not moved to itself.

You’re doing it wrong™ :-)

I did not say do it, I just mentioned the possible problems :-)

Agreed. But one has those with every signal/slot connection once threads are involved.

 
  ‹‹ Hide shorcut text of QMenu Action and more ;)      [Moved] highlighting the icon ››

You must log in to post a reply. Not a member yet? Register here!