PDA

View Full Version : Invoking a slot in another thread



dorik
8th July 2011, 23:10
Hello,

I am trying to use invokeMethod from the main thread on the slot of a QThread subclass which is running an event loop.


QMetaObject::invokeMethod(myThread, "mySlot", Qt::QueuedConnection, Q_ARG(const SomeObject&, obj));

However, outputting QThread::currentThreadId() inside the slot shows that it executes in the main thread. Does anyone know what the problem is? I would prefer not to connect a signal to the slot.

Thanks!

squidge
8th July 2011, 23:25
You'll probably need to post some of your threading code, but have you tried moveToThread?

dorik
9th July 2011, 00:25
You'll probably need to post some of your threading code, but have you tried moveToThread?

How would moveToThread help in this case? Do you mean move the QThread object into its own thread?

DanH
9th July 2011, 03:57
Which thread owns the QThread? Likely you created it in the main thread and it's still owned by the main thread, so that's where any slots will be executed.

dbzhang800
9th July 2011, 04:08
http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/

dorik
11th July 2011, 18:42
Ah, I understand now. My QThread object lives in the main thread, but I thought signals and slots for the QThread would execute inside it's thread context. Thanks for the link, dbzhang800.