PDA

View Full Version : signal getting caught in another event loop



raj_iv
29th September 2012, 19:58
In my program i am using QHttp subclass for http requests. As QHttp does not provide blocking functions i m using local event loop to handle blocking functionality. I am connecting requestfinished() signal to a slot which will later quit th event loop.

But in addition to that i have another class which also uses QHttp and there also requestfinished() signal is connected to some slot. This class is running in main event loop.

Now wat happening is that when i started my local event loop from above class the signal is getting caught in main event loop instead of it getting caught in my local event loop. Wats the behavior of same signal and slots across different event loops in Qt. Is tat if same signals connected to some slots but in different eventloop, the signal may get caught in any of the event loop?

To solve this problem is there any thing i can do or i have to use seperate thread for my need.

amleto
29th September 2012, 20:11
Signals dont get stuck in event loops. signals don't care about which event loop they enter. Slots tend to care a bit more, but signals... not so much.

"Now wat happening is that when i started my local event loop from above class the signal is getting caught in main event loop instead of it getting caught in my local event loop"
It wont make any difference. If the loops are in different threads, then you may have a problem stopping some slots from running (since slots only execute in their own thread when executed from a signal (normally)).

"Wats the behavior of same signal and slots across different event loops in Qt"
different event loops makes no difference if they're in the same thread.

raj_iv
29th September 2012, 20:31
Ok amleto,

So according to ur post what i understand is that signals dont care about event loops. A signal get generated and then a slot get fired; now which slot would get executed? If i initiated the request from my local eventloop then the slot in my local loop should get fired; but th slot in main event loop gettin fired. So does tat mean Slots also dont care about eventloops as they r in same thread.

wysota
29th September 2012, 20:35
This class is running in main event loop.
There is only one event loop (per thread). QEventLoop only provides a different entry point to the existing event loop. Events do not care if they are handled as a result of QEventLoop::exec() or QCoreApplication::exec(). The only difference is that calling exec() on QEventLoop bumps up an internal level counter in the event loop which cares that deferred delete (aka QObject::deleteLater()) is executed on the same (or lower) level at which the delete was scheduled. In other words, if you do this:

QObject *obj = ...;
obj->deleteLater();
QEventLoop loop;
loop.exec();
bool b = obj->isStillThere();

"obj" is still valid after loop.exec() returns.