How to call a function periodically in Qt?

后端 未结 5 1277
耶瑟儿~
耶瑟儿~ 2021-02-14 22:10

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 ?

相关标签:
5条回答
  • 2021-02-14 22:49

    As people have said in answers before me, you can use the timeout() signal to trigger a 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.

    0 讨论(0)
  • 2021-02-14 23:06

    Make a function that uses timer functionallity or a while loop that just waits for 100 ms and when your function meets the requirement just break. You could quite easy found a solution on this one if you just made a search among all the other questions that has been posted here.

    0 讨论(0)
  • 2021-02-14 23:09

    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);
    
    0 讨论(0)
  • 2021-02-14 23:09

    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().

    0 讨论(0)
  • 2021-02-14 23:14

    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().

    0 讨论(0)
提交回复
热议问题