开发者

Qt: Worker thread that has slots and emitters on main thread

开发者 https://www.devze.com 2023-03-23 15:45 出处:网络
Say I wanted to have a worker thread that has slots for signals emmited from the main application thread.

Say I wanted to have a worker thread that has slots for signals emmited from the main application thread.

  1. 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?

  2. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号