Polling a variable in QT once every second

不羁岁月 提交于 2019-12-23 19:26:15

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/15556315/polling-a-variable-in-qt-once-every-second

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!