Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Call function periodically (i.e. daily)
Forum Updated to NodeBB v4.3 + New Features

Call function periodically (i.e. daily)

Scheduled Pinned Locked Moved Solved Mobile and Embedded
timer
12 Posts 3 Posters 6.6k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mcosta
    wrote on last edited by
    #2

    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
        {
            myfunction();
            m_lastExecutionTime = nowUtc;
        }
    }
    

    Once your problem is solved don't forget to:

    • Mark the thread as SOLVED using the Topic Tool menu
    • Vote up the answer(s) that helped you to solve the issue

    You can embed images using (http://imgur.com/) or (http://postimage.org/)

    1 Reply Last reply
    1
    • McLionM Offline
      McLionM Offline
      McLion
      wrote on last edited by
      #3

      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.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mcosta
        wrote on last edited by
        #4

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

        Once your problem is solved don't forget to:

        • Mark the thread as SOLVED using the Topic Tool menu
        • Vote up the answer(s) that helped you to solve the issue

        You can embed images using (http://imgur.com/) or (http://postimage.org/)

        1 Reply Last reply
        0
        • McLionM Offline
          McLionM Offline
          McLion
          wrote on last edited by McLion
          #5

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

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

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #6

            Hi,

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

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • M Offline
              M Offline
              mcosta
              wrote on last edited by
              #7

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

              Once your problem is solved don't forget to:

              • Mark the thread as SOLVED using the Topic Tool menu
              • Vote up the answer(s) that helped you to solve the issue

              You can embed images using (http://imgur.com/) or (http://postimage.org/)

              1 Reply Last reply
              0
              • McLionM Offline
                McLionM Offline
                McLion
                wrote on last edited by
                #8

                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

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  @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

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mcosta
                    wrote on last edited by
                    #10

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

                    Once your problem is solved don't forget to:

                    • Mark the thread as SOLVED using the Topic Tool menu
                    • Vote up the answer(s) that helped you to solve the issue

                    You can embed images using (http://imgur.com/) or (http://postimage.org/)

                    1 Reply Last reply
                    0
                    • McLionM Offline
                      McLionM Offline
                      McLion
                      wrote on last edited by
                      #11

                      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

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        Nice !

                        @McLion Schöni Platz ! :)

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved