Question to QEventLoop
-
Hello,
I got a question regarding a QEventLoop.What happens if a signal connected to a QEventLoop, which is not executed yet, is emitted?
In my case I trigger a function in another thread with a signal and then I want to wait for a signal that the function emits when it is finished.
Not I could imagine, that my function in another thread could maybe emit the signal before I called "loop.exec();"
So is the signal even then already "stored" in my loop and automatically processed when calling "exec" or do I need to do something else to make sure that the signal is not lost?This is an example:
QTimer timer; timer.setSingleShot(true); QEventLoop loop; connect(&worker, SIGNAL(workInOtherThreadDone()), &loop, SLOT(quit()) ); connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit())); emit doSomethingInOtherThread(); //Triggers function in other thread which then emits "workInOtherThreadDone" timer.start(1000); loop.exec(); if(timer.isActive()) { //do something } else qWarning("timeout!");
Thank you very much :-)
-
So is the signal even then already "stored" in my loop and automatically processed when calling "exec"
Short answer: yes.
Explanation: since loop lives in a different thread from the one emitting the signal the execution of slots will be delayed untill control goes back to any event loop in the receiving thread. In your case this happens when you call
loop.exec();