[Qt-interest] Best practices for a thread object

Etienne Sandré-Chardonnal etienne.sandre at polytechnique.org
Sun Jan 16 16:50:05 CET 2011


Dear all,

In my application, I define a "Worker" class, which inherits QThread. This
worker class lives its own life in its thread and does computations.

My main GUI application thread creates a myWorker instance of this worker
class and call Worker::run().

This means, the myWorker instance belongs to the main app thread from which
it was created. Then, within Worker::run(), I cannot create children
QObjects, as this gives me the classical error message:
QObject: Cannot create children for a parent that is in a different thread.

I fully understand this error message. But what would be the best practices
for doing this?

I'm thinking of using moveToThread for this, such as:

MainApp::startComputation()
{
    myWorker = new Worker();
    myWorker->moveToThread(myWorker);
    myWorker->run();
}


Is it possible or stupid to move a QThread to itself with moveToThread?
Should I do this before or after calling run() ?

What is the exact meaning of the belonging of a QObject to a QThread? I do
not see which mechanism is involved.

Thanks in advance,

Etienne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110116/5c4b42a8/attachment.html 


More information about the Qt-interest-old mailing list