Gerstmann
Gerstmann

Reputation: 5518

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

Upvotes: 0

Views: 2524

Answers (3)

Moystard
Moystard

Reputation: 1837

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.

Upvotes: 1

hmuelner
hmuelner

Reputation: 8221

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().

Upvotes: 2

Marker
Marker

Reputation: 31

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().

Upvotes: 0

Related Questions