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?
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
QTimeclass. Each time your signal is emitted, callQTime::startand then check theQTime::elapsedvalue. 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
QVectoror 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
QTimerclass might be useful in this case–have it poll the sensor (and emit the signal) every time it “ticks”.