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 ?
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);
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()
.
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With