PDA

View Full Version : QThread event loop blocking the GUI



JoeMerchant
17th July 2009, 17:04
Hi,

I have an app with a big, nicely functioning, GUI that I want to add some threaded processing to, using threads to draw things that take a long time, and since there may be multiple things that take a long time, multi-core systems should draw faster - I hope....

There are a few (like 3 to 8) of these widgets, each creating their own "DrawingThread" which runs the default event loop. When the widgets draw quickly, everything is fine, but if the drawing thread is taking a long time, the main GUI hangs, timers don't fire, etc. I have added "yieldCurrentThread()" into the worker function in the drawing thread, but that seems to do nothing. Adding msleep( 1000 ) causes the drawing thread to pause for 1 second, but the GUI remains hung during this pause. The worker function is a slot in the DrawingThread object and is fired by a signal emitted by the GUI. In turn, it fires periodic signals to slots (repaint()) in the GUI, and these are being done, but nothing else. So, I see my slow drawing screen slowly work across, while everything else is frozen... Once the drawing is done, everything is snappy again. To be clear, the GUI isn't sluggish, it's dead stopped. Setting the DrawingThread priority down doesn't do anything except cause my app to fall behind others in the system for priority - the GUI still stays hung.

I've read all about thread affinity, and as far as I can tell, this is it's own thread. I have other threads in the app where I re-implemented the run() and they all work as expected with the GUI continuing nicely while they work, they produce signals but do not consume them. This is my first thread to use the default run() {exec();}, and that seems to be the difference, due to the implementation, these threads need to receive signals....

It seems like I just have a fundamental implementation issue - any ideas what it might be?

Thanks,

caduel
17th July 2009, 17:57
maybe you are waiting for your threads or something?
Show us some code and we can really help.

wysota
17th July 2009, 18:10
You can't render widgets from external threads.

caduel
17th July 2009, 19:04
Wysota is right, of course.
(You could render to a QImage in a separate thread and then just paint the QImage in the main thread. So the calculations - which should be where the time is consumed - can be done in a separate thread, but the actual painting can not.)

wagmare
18th July 2009, 07:54
QPainter can be used to paint onto QImage, QPrinter, and QPicture paint devices. Painting onto QPixmaps and QWidgets is not supported.