How to periodically call a function in Qt?

Is it possible to periodically call a function in C ++ using the Qt function?
And how to stop the synchronized function after it will be called periodically?

+6
source share
5 answers

If you use qt, you can use QTimer, which by default creates a repeating timer.

In the documentation (see below) there is an example (Analog Clock).

QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(update())); timer->start(1000); 
+14
source

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

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

To stop the timer, call stop() .

+2
source

You can use the QTimer class.

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

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

+1
source

As people said in my answers, you can use the timeout signal () to run the function to run.

If you want to stop the timer at some point, you can connect to the stop () slot or call it directly yourself.

+1
source

Create a function that uses a timer function or a while loop that just waits for 100ms, and when your function satisfies the requirement, just take a break. You could easily find a solution to this if you just searched among all the other questions that were posted here.

0
source

Source: https://habr.com/ru/post/945992/