[Qt-interest] QThread event loop running in main thread?
Robert Escott
robert at binarylogic.co.za
Tue Dec 28 15:19:02 CET 2010
The QThread class does not represent a new thread. It is a thread manager. Only object instances created inside the run() function or in other functions called from the run() function exist in another thread.
What this means is that all the functions/signals/slots of the QThread or derived class exist in the same thread that the QThread object was created.
In your example, myThread is in the same thread as MainWindow. myTimer is created inside the run() function and thus exists inside a new thread.
If you want tweak() to run in a separate thread from tweakThread(), you need to have an object created inside the run() function and create the connection there, exactly the same way as you did with myTimer.
The line,
connect(myTimer, SIGNAL(timeout()), this, SLOT(tock()));
will cause a signal to cross a thread boundary.
From: qt-interest-bounces+robert=binarylogic.co.za at qt.nokia.com [mailto:qt-interest-bounces+robert=binarylogic.co.za at qt.nokia.com] On Behalf Of lbutler3 at comcast.net
Sent: 27 December 2010 06:05
To: qt-interest at qt.nokia.com
Subject: [Qt-interest] QThread event loop running in main thread?
I seem to be having trouble with a QThread and signals/slots. I noticed this when the Ui for my application stopped responding when a thread got busy. I've boiled it down to the fact that the thread event loop seems to be running in the main thread.
In my main window constructor I do:
connect(this, SIGNAL(tweakThread()), &myThread, SLOT(tweak()), Qt::QueuedConnection);
myThread.start();
The run method for the thread basically just calls:
this->exec();
When the main window does:
emit tweakThread();
the tweak() method on myThread runs immediately in the main thread of execution, not in the thread of execution of myThread.
In my full application I'd like to do some extended calculations in the thread tweak() routine without hanging the user interface.
Is this the expected behavior? Is there a way to get the thread's event loop to run in the thread?
Lee
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20101228/bc0aead9/attachment.html
More information about the Qt-interest-old
mailing list