[Qt-interest] QThread event loop running in main thread?

sarvesh saran aquarian.thunder at gmail.com
Mon Dec 27 07:05:33 CET 2010


Hi,

QObjects  have an affinity with a single thread. This is usually the thread
that creates the QObject. So if you create a QObject in the app's main
thread and want to use it in another thread, you need to use
moveToThread()  to change the affinity.

 Create your QObject (containing the logic of your tweak() method), connect
your signals, create your QThread, move your QObject to the QThread and
start the thread. The signal/slot mechanisms will ensure that thread
boundaries are crossed properly and safely.

Check this link for an example:

http://labs.qt.nokia.com/2006/12/04/threading-without-the-headache/

<http://labs.qt.nokia.com/2006/12/04/threading-without-the-headache/>thanks,
Sarvesh Saran


On Mon, Dec 27, 2010 at 9:35 AM, <lbutler3 at comcast.net> wrote:

> 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
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20101227/7d0c4448/attachment.html 


More information about the Qt-interest-old mailing list