Say I wanted to have a worker thread that has slots for signals emmited from the main application thread.
How w开发者_StackOverflow中文版ould the "::run()" method of that worker thread look? obviously I need some kind of loop or the thread will terminate immediately. I want it to stick around so it can process the incoming slots. How would that loop look like? Something equivalent to sleep and pump messages? pump how?
Do I need to "MoveToThread()" all the objects that'll pass to the thread's slots so that the processing of the slot is done in the context of the worker thread?
Thank you.
Did some more digging. The default implementation of QThread::run calls “exec” which is the message loop. So no extra work needed there. In the constructor of my QThread I add this:
this->start();
QObject::moveToThread(this);
As a result, my thread starts upon construction, and all signals outside the thread aimed at thread slots are executed in the context of my thread.
精彩评论