[Qt-interest] QThread problem
Oliver.Knoll at comit.ch
Oliver.Knoll at comit.ch
Wed Nov 3 15:06:17 CET 2010
On 2010-11-03 Joshua Joshua Wambua wrote:
> Why don't you use a timer?
Timers are lame! Real men use threads ;)
No seriously, the Qt Mandelbrot examples/threads/mandelbrot nicely shows how to have a worker thread update the GUI with queued signals.
Note the c'tor of the MandelbrotWidget:
connect(&thread, SIGNAL(renderedImage(QImage,double)),
this, SLOT(updatePixmap(QImage,double)));
The 5th parameter of connect() defaults to Qt::AutoConnection, and since the instances 'thread' and 'this' live in *different* threads (technically: when http://doc.qt.nokia.com/4.7/qobject.html#thread returns different values for the instances to be connected) this will evaluate to Qt::QueuedConnection.
And that is what you want: the worker threads tells: "Hey, I got some new data, please render it". But since we all know that with Qt (and most other GUI frameworks) the widgets must not be updated within any other thread than the main thread (technically: where the QApplication instance "lives" in, aka "GUI thread"), the actual slot - which draws the widgets etc. - must be executed within the GUI thread. And that is exactly where the Qt::QueuedConnections are helpful!
But keep in mind that Queued Connections don't help you in avoiding concurrent access to your data! Read about QSemaphore and Co.: http://doc.trolltech.com/4.7/threads.html
Cheers, Oliver
--
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22
More information about the Qt-interest-old
mailing list