user1041903
user1041903

Reputation: 205

Qt: GUI sometimes freezing when using threads and signals/slots

I have two (alternative) functions working on data supplied by a sensor. The functions run in their own thread and emit a signal when the result is ready. This signal is connected to a slot of a UI widget, displaying the result. With one of the functions, this works well. When using the other however, the GUI starts to lag and soon freezes.

QDebug shows that data is still being processed though.

When running the code in the GUI-thread, there is no problem.

Might the problem be that the worker-thread produces data faster than the UI can draw it, leading to some lag due to the Qt::QueuedConnection? If so, what alternatives do I have? If not, what else can I check?

Upvotes: 2

Views: 2844

Answers (2)

user1041903
user1041903

Reputation: 205

I actually just managed to find a solution: The data processing, done in a separate thread was polling the sensor class way to often. Therefor, the signal to the UI thread was emitted several times for each sample from the sensor.

By including a QWaitCondition into the processing thread, I managed to get down to a more reasonable refresh rate.

Nevertheless, many thanks for your answer.

Upvotes: 0

Dave Mateer
Dave Mateer

Reputation: 17946

It would appear that the worker thread is spamming the UI thread, filling up the main event loop so that GUI events have a hard time getting processed.

Without seeing some of the code in the worker thread, it is difficult to recommend a solution. At the end of the day, you only want to emit your signal at specified intervals.

You may have some luck with the QTime class. Each time your signal is emitted, call QTime::start and then check the QTime::elapsed value. Only when it is above a certain threshold emit your signal and reset the timer.

If you can throw away the intermediate sensor values, that's ideal. If you need them all, you would have to add them to a QVector or something and send them all at once in the signal.

Even better is if you can only poll the sensor itself at regular intervals. The QTimer class might be useful in this case--have it poll the sensor (and emit the signal) every time it "ticks".

Upvotes: 4

Related Questions