[Qt-interest] QThread event loop running in main thread?
Thiago Macieira
thiago at kde.org
Tue Dec 28 20:56:21 CET 2010
On Tuesday, 28 de December de 2010 07:27:14 Mihail Naydenov wrote:
> Well, the point I argue about is, that taking the Forbidden Step - in your
> example myThreadWorker : public QThread
> and get rid of workerThread field is actually worth it - no need for stubs
> to control the thread, access to sleep(), instantiate and mange two
> instead of three QObjects.
No, that's not the forbidden step.
Deriving from QThread is completely legal and ok. Don't go talking about how
that is wrong.
The documentation is still correct, even if incomplete. You don't need to
derive from QThread in order to use it. But you can.
What is *forbidden* is moveToThread(this). And because of that, more often
than not, using slots in a class derived from QThread is often *wrong*.
Wrong because it doesn't do what you think it does. Since the QThread object
was created, obviously, in another thread, that's the thread to which events
will be posted. So that's the thread where slots will be called in, due to
queued connections.
So this code in the run() function:
QTimer::singleShot(0, this, SLOT(someSlot()));
exec();
will probably not do what you think it does! The slot "someSlot" will be
called in the original thread, not in the thread that this QThread object
started.
Using signals in a class derived from QThread isn't wrong. It's just a concept
violation. A signal called "workDone" means the job is done, not that the
thread is done. Otherwise, you could just use the "finished" signal.
--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Senior Product Manager - Nokia, Qt Development Frameworks
PGP/GPG: 0x6EF45358; fingerprint:
E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20101228/aa8487fe/attachment.bin
More information about the Qt-interest-old
mailing list