Qt World Summit: Register Today!

Call function periodically (i.e. daily)

  • Hi

    I need to execute a function daily or twice a day. QTimer cannot be used for such "long" events.
    Is there any other way available in Qt (4.6.3 on eLinux) that can be used to easily set this up ... or do I have to create something myself?


  • You can Use QTimer with a small Interval (1 minute, 1 hour) and when it expires you can check if you need to execute the function. (you can store the last execution time)

    void MyClass::slotTimerExpired()
        QDateTime nowUtc = QDateTime::currentDateTimeUtc();
        if (m_lastExecutionTime.secsTo(nowUtc) >= 86400) // 1day = 86400 seconds
            m_lastExecutionTime = nowUtc;

  • Thanks.
    Is the int used in QTimer 32 bit wide?
    I thought it is 16 bit only. If it is 32 bit wide it would be even possible to set the timer interval to a whole day as well.

  • QTimer interval in a int 32bit but is in msec; so you cannot use a day interval.

  • ... of course not ... that was some "miscalculation in my head" ... sorry

    .. on second thought ... works perfectly for a whole day

  • Lifetime Qt Champion


    Since you're running Linux, why not setup a cron job for that ?

  • To use cron he should provide an external interface (executable to be run)

  • Correct.
    I just need to execute a single function in my Code periodically.
    btw: works perfect with the timer with an interval of a day

  • Lifetime Qt Champion

    @mcosta I agree, but depending on what that function should do, having a little helper program that can run without the need of the complete application might also be interesting. A cron job doesn't need to be built program, it can be written in e.g. python

  • @SGaist Agree, a cron job IMO is the best solution. I'm only answering to the original question "calling a function"

  • Actually, the cron job would have been perfect for most of the periodic task, yes.
    The periodic task is:

    • Download a file (SSL CA cert-bundle) from TFTP server (perfect for cron)
    • Compare to current local copy (perfect for cron)
    • If changed, replace local copy (perfect for cron)
    • and then update QSsl with QSslSocket::addDefaultCaCertificates

    Anyhow, I finished it in Qt and it works just perfect.
    Thank you guys!

    ..@SGaist : Grüess is Wallis us de Zentralschwiiz

  • Lifetime Qt Champion

    Nice !

    @McLion Schöni Platz ! :)

Log in to reply