I'm trying to use threads in Qt to delegate some work to a thread, but I can't get it to work. I have a class inheriting QMainWindow that have a member object that launch threads to do work. This object has the QMainwindow as parent. It contains and initialize notably another QObject, the m_poller
, which I want to move to the thread I create :
m_pollThread = new QThread;
m_poller->moveToThread(m_pollThread);
//Bunch of connection
m_pollThread->start();
I followed the guidelines about how to manage thread in Qt without subclassing it (aka not doing it wrong), but I still get the following message in VS :
QObject::moveToThread: Current thread (0x2dfa40) is not the object's thread (0x120cf5c0). Cannot move to target thread (0x1209b520)
I found the following post that seems to deal with the same issue, but couldn't fix my code with the answer. I feel like I'm actually calling moveToThread correctly (as in I don't call it from within another thread to "pull" an object to it), but apparently I'm still missing something there: as the message hints, it seems there's already multiple thread and my call to moveToThread() seems to end up in the wrong one (though I admit I'm completely new to this and could figure this out completely wrong...)
So what could still be possibly wrong with the way I use Qt threads ?
Thanks !
You can only use moveToThread
in case when
So your error message says you're violating the second case. You should call moveToThread
from the thread that created the object.
And according to you
This object has the QMainwindow as parent.
So moveToThread will not work, again. You should remove the parent from m_poller object
I think the problem is with the initialization of m_poller, which according to the error message seems to be assigned to a different (third) thread from the one that is executing your code snippet.
Also, if this code is executed multiple times, it may work the first time but then fail on subsequent times as m_poller no longer belongs to the executing thread, but rather m_pollThread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With