Invoking a slot in another thread
Hello,
I am trying to use invokeMethod from the main thread on the slot of a QThread subclass which is running an event loop.
Code:
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!
Re: Invoking a slot in another thread
You'll probably need to post some of your threading code, but have you tried moveToThread?
Re: Invoking a slot in another thread
Quote:
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?
Re: Invoking a slot in another thread
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.
Re: Invoking a slot in another thread
Re: Invoking a slot in another thread
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.