Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polling a variable in QT once every second

I'm working on a multithreaded image processing application. I added on a GUI based on QT for the purposes of changing some parameters that I have to experiment with a lot rather than loading them all from a settings file each time I start the program or having to type them in. I would also like the GUI to display some basic information from each of the threads so I can monitor them. I currently have thread safe methods of passing information between the image processing threads set up and I would like a way to poll some of this information from the QT thread approximately every second so I can display some feedback on the UI.

My requirement is that I don't want to have to incorporate QT specific code into the image processing threads to update the UI. I would rather have the UI thread poll the methods I currently use to pass information between the threads. I want the image processing portion of my codebase to stand alone and not have to depend on QT to run. How would I poll a globally available function to update the QT UI?

like image 697
Danny Avatar asked Feb 18 '23 02:02

Danny


1 Answers

QTimer is your friend.

QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), &someQObjectDerivedClassInstance, SLOT(doYourThing()));
timer->start(1000);

Or in Qt5 and C++11 you can directly connect to a lambda. Although using a slot will ensure you get queued connections in case you are connecting to an object living in another thread.

like image 153
dtech Avatar answered Feb 20 '23 10:02

dtech