PDA

View Full Version : processEvents() for a QThread



hb
24th September 2007, 08:26
I am using a QThread to do some large-scale computation in a worker thread. If there is nothing to do, however, the worker thread is waiting in a wait condition, similar to how it is done in the Mandelbrot example. My thread class works well and has non-trivial logic (when to enter an event loop, exit it again, go to sleep in the wait condition etc), , so I would like to keep it that way.

Now I realized that I need cross-threads (queued) signal/slot connections for objects that live in the worker thread. Of course, the slots are not executed if the thread is currently in a wait condition.

My idea was now to add a function to the thread class that would execute all pending events, and then go to sleep again in the wait condition. That way, I could emit my signals, and then make sure to wake up the thread to execute the corresponding slots.

Unfortunately, although QThread have an event loop, there is no such thing as QThread::processEvents(). Is there any workaround to achieve what I want?

wysota
24th September 2007, 08:58
All you need is to wake the thread and create a single shot timer with a 0 timeout. The timeout slot will be called after all pending events are processed (due the nature of timer implementation) and you can put the thread back to sleep again there.

hb
25th September 2007, 07:42
Thank you once again, wysota. Your proposed solution works flawlessly.

DesertF0x
17th March 2011, 15:46
Seems like it is not working this way anymore. Any Ideas?

Edit:
Ah, it is because I am trying to interrupt a slot doing heavy work. Seems like I have to use processEvents().