PDA

View Full Version : QThread event loop seems blocked



eurodatar
6th May 2009, 17:19
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:



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?

wysota
6th May 2009, 17:32
If you have a while loop then you don't give the event queue the chance to process events.

eurodatar
6th May 2009, 17:39
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
postEvent(objectInGuiThread, MyEvent). That way I *think* that the GUI event loop will take care of everything.

wysota
6th May 2009, 17:50
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 (http://doc.trolltech.com/qq/qq27-responsive-guis.html).