Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a function periodically in Qt?

Tags:

c++

qt

Is it possible to call a function periodically in C++ with Qt function ?
And how to stop the timed function after it is set to be called periodically ?

like image 345
CDT Avatar asked May 28 '13 07:05

CDT


3 Answers

If you are using qt, you can you QTimer which by default creates a repetitive timer.

There is an example in the documentation (shown below) and an example (Analog Clock).

QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);
like image 181
parkydr Avatar answered Sep 30 '22 11:09

parkydr


One possibility would be to use a QTimer timeout signal and a QObject slot. Connect the two and start() the timer.

http://qt-project.org/doc/qt-4.8/qtimer.html#timeout

To stop the timer, call stop().

like image 33
user1095108 Avatar answered Sep 30 '22 10:09

user1095108


You can use the QTimer class.

Just declare a QTimer with the desired time interval, wrap your function in a QObject as a slot, and connect the QTimer's timeout() signal to the slot you just declared.

Then, when the condition for stopping calling the function is met, just call QTimer::stop().

like image 41
JBL Avatar answered Sep 30 '22 09:09

JBL