QThread event loop seems blocked
Hi,
I've read about threads and signals in some other topics but I couldn't really find something similar to this.
I have a worker thread that is supposed to ping the GUI with information every now and then. The algorithm is similar to:
Code:
while(hasWork())
{
emit Status()
DoWork()
emit Status()
}
The problem is that the slots connected to Status are only executed after all the work is done and the while loop has exited.
Considering that:
- I'm calling exec() in QThread::run()
- I'm using queued connections
- I'm starting the work by emitting a signal. The signal is connected to the slot of an object that lives in the thread and the slot is executed in the thread.
- the slot and its parent object live in the GUI thread.
... I think that the thread's event loop is blocked while executing the slot that started the thread and can't process my emitted signals.
Any ideas on getting around this issue?
Re: QThread event loop seems blocked
If you have a while loop then you don't give the event queue the chance to process events.
Re: QThread event loop seems blocked
Yes, I was afraid of that... Is there a Qt pattern for situations like these when you want to issue updates to the GUI from a worker thread while in the middle of a big job?
I was thinking of removing the event loop altogether and just calling
Code:
postEvent(objectInGuiThread, MyEvent).
That way I *think* that the GUI event loop will take care of everything.
Re: QThread event loop seems blocked
You don't need an event loop in the thread you are emitting signals from - only in the one you are emitting them to.
Anyway, have a look at this article: Keeping the GUI Responsive.