Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to delay QTimer start?



  • I have the QTimer working with proper connect and interval of 1-minute. I would like for it to start at the top of the minute, i.e. when the time has 0 seconds, so that it will be called every minute on time such at 9:31, 9:32:00, 9:33:00..... If the program starts at 9:30:43 for example I want the timer to start at 9:31:00. Is there a simple way to do this?

    The only way I can think of is to fire a single shot timer which waits till 9:31:00 in the above example to start the 1-minute timer. Thanks.



  • @ACollins
    Yes, the initial singleshot to delay when it starts till the minute arrives is the usual way.

    Note also the following about accuracy. I imagine that, once started, you intend the timer to keep expiring on the minute for an extended period of time. However, timers can expire late, or even early. Read https://doc.qt.io/qt-5/qtimer.html#accuracy-and-timer-resolution. The default QTimer is https://doc.qt.io/qt-5/qt.html#TimerType-enum

    Qt::CoarseTimer 1 Coarse timers try to keep accuracy within 5% of the desired interval

    You might want to improve to Qt::PreciseTimer (though I'm not sure that is intended for this kind of job).

    I am unsure whether an early/late expiry is "cumulative", i.e. the next timer interval starts from when it did expire, rather than when it should have expired. If it's the former then your timeout can "wander" over time.

    I think you'd have to try it setting it off for a day, with the computer doing various things, to discover. If it does wander unexpectedly, I imagine the approach is: set the timer repeating interval, in the timeout check the system clock, if it has wandered too far stop the timer and correct for next interval and restart, repeating this adjustment at next interval till it settles down to close enough again.

    Finally, if you have that last part you can probably use it to avoid the initial singleshot of your question: you could set the first timer to 17 seconds ( 9:30:43 -> 9:31:00), for example, and allow it to "correct" on first expiry.


  • Qt Champions 2019

    @JonB said in How to delay QTimer start?:

    I am unsure whether an early/late expiry is "cumulative", i.e. the next timer interval starts from when it did expire, rather than when it should have expired. If it's the former then your timeout can "wander" over time.

    It's not :)


Log in to reply