开发者

Qt - Worker thread freezes the UI

This is my first experience on threading with Qt, so bear with me.

I have a singleton "system" object which periodically executes a heavy piece of code. I control the system singleton from my UI, but the system is not aware of the UI.

I create a thread in my main, and then move the system to it:

QThread systemThread;

System::instance()->moveToThread(&systemThread);

systemThread.start();
qApp.exec();

The UI hangs until the system's periodical开发者_StackOverflow processing cycle is complete.

I have also tried to subclass QThread and calling exec from the run method.

What could be the problem? I'm certainly doing something wrong.

Best regards


See the excellent article about Threads, Events and QObjects in the Qt developer wiki. Something seems to be wrong with the thread affinity, you can check that with QObject::thread().


I advice you to follow this guideline from a Qt Developer himself when implementing multithreading: https://www.qt.io/blog/2010/06/17/youre-doing-it-wrong

It is way more effective and painless.


systemThread.start() will start systemThread.run() method in thread so you need to implement it inside run().

To create your own threads, subclass QThread and reimplement run().

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜