Important: Please read the Qt Code of Conduct -

Experience with QTimer and long time spans

  • Hello,

    I need three timers for some actions in a time span
    between days and weeks.
    Is it possible or does it make sense, to take a singleshot timer
    and set the timespan I need. Or should I use counters and one
    timer with minute or hour clock?
    I have to pay attention on the memory usage, because
    this is an embedded system

  • How about this: Set a timer to an interval of, say, 10 Minutes (or even an hour when you don't need such fine grained control). In the timeout() slot, you check whether QDateTime::currentDateTime() is greater than a QDateTime that you've set up to point at the desired time in the future. If this is the case, execute whatever you need.

    If you want a more sophisticated and flexible approach, make an abstract functor class and a QMap<QDateTime, AbstractFunctor>. In the timer slot, iterate through the map from the beginning until the map key is greater than the currentDateTime, execute and then remove the functors from the map. This way your future events can even survive program/system shutdown without trouble by saving the map in a destructor/closeEvent.
    When adding new events, create the desired functor subclass, set any state parameters that shall define the behaviour at the event, and add it to the map.
    Tip: in the timer slot, set the timer interval to the maximum of ~1h and the difference of the current time to the next event. This way you have persistent, exact timing for multiple events without the need for multiple timers with huge intervals.

  • Do you actually know your application is going to run for this time?

  • @DerManu: thank you for your answer, I hope I have understood, what you meant.
    @Andre: this application should be a controller for a weather station and should run "endless".

  • [quote author="gandiii" date="1336748880"]this application should be a controller for a weather station and should run "endless".[/quote]
    Oh that's nice. Our measurement stations (not weather though) have crashed regularly in the first years of development. Do you plan to implement remote firmware/software updates? If not, happy hiking with a screwdriver every evening ;). If yes, good, then you'll need persistent timed events, though.

Log in to reply