What does it mean to move a object from one thread to another in Qt using moveToThread? Everything seems to work even before using moveToThread, which moves the object from
The QThread::start()
method creates the thread and calls your run()
implementation. If you want to handle events or received signals on the thread you have to call QThread::exec()
inside your run()
implementation. You should never call run()
explicitly and you should never call exec()
outside of run()
.
The owner thread makes a difference only when a slot is connected to a signal with the connection type other than Qt::DirectConnection
. Then Qt will ensure that the slot runs on the owner thread, but for that the owner thread must be running an event loop with QThread::exec()
. In this case calling myObj.moveToThread(myThread)
will ensure that myObj
slots run on the thread myThread
.
The thread object belongs to the thread where it was created, not on the thread that it manages (and where the run method will run). So when you connect a signal to a thread object's slot, that slot will run in the thread where the thread object was created unless you call moveToThread()
.