Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to create a watchdog timer in Qt
Qt 6.12 LTS is out! See what's new in the release blog

How to create a watchdog timer in Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtimer watchdog
13 Posts 7 Posters 14.0k Views 5 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
    mjsurette
    wrote on last edited by
    #3

    I'm sorry, I thought that it was a common term. I guess not.

    A watchdog timer is one that counts down from a preset value. Incoming events can reset it to its original value and thereby preventing it from timing out. If there is an absence of triggers for long enough it times out and triggers an action.

    In my case, I have updates to some data that comes in bursts of indeterminate size. I would like to wait until there have been no updates for 10ms or so before updating the screen so they can all be handled in a batch. This would be much more efficient.

    I originally thought that a QTimer in singleshot mode would do the trick, but you can only trigger it once and there is no way to restart it.

    ? T mrjjM 3 Replies Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #4

      Hi,

      QTimer is the right tool. Make one member of your class, configure it as single shot and you can then restart it as needed.

      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 mjsurette

        I'm sorry, I thought that it was a common term. I guess not.

        A watchdog timer is one that counts down from a preset value. Incoming events can reset it to its original value and thereby preventing it from timing out. If there is an absence of triggers for long enough it times out and triggers an action.

        In my case, I have updates to some data that comes in bursts of indeterminate size. I would like to wait until there have been no updates for 10ms or so before updating the screen so they can all be handled in a batch. This would be much more efficient.

        I originally thought that a QTimer in singleshot mode would do the trick, but you can only trigger it once and there is no way to restart it.

        ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #5

        @mjsurette said:

        there is no way to restart it

        You can restart the timer by calling [slot] void QTimer::start() again.

        1 Reply Last reply
        1
        • M Offline
          M Offline
          mjsurette
          wrote on last edited by
          #6

          I must be missing something.

          This simple test program should have two timeouts, at 6 and 16 seconds in. But only one registers, and that's at the close of the program.

          #include <QCoreApplication>
          #include <QTimer>
          #include <QDebug>
          #include <QDateTime>
          
          #include <unistd.h>
          
          qint64 starttime;
          
          void timeout()
          {
              qDebug() << "timeout:" << QDateTime::currentMSecsSinceEpoch()-starttime;
          }
          
          int main(int argc, char *argv[])
          {
              QCoreApplication a(argc, argv);
          
              QTimer wd;
              wd.setSingleShot(true);
              QObject::connect(&wd,&QTimer::timeout,&timeout);
          
              starttime= QDateTime::currentMSecsSinceEpoch();
          
              qDebug() << "start:" << QDateTime::currentMSecsSinceEpoch()-starttime;
          
              for (int i=0;i<20;++i)
              {
                  qDebug() << i;
                  if (i==1 || i==11)
                      wd.start(5000);
                  sleep(1);
              }
              qDebug() << "done:" << QDateTime::currentMSecsSinceEpoch()-starttime;
              return a.exec();
          }
          
          
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #7

            The event loop is not running while you're in that for loop so your timer will only run once.

            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
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #8

              I must be missing something.

              Timer events are processed in the event loop (exec() call). You restart the timer 20 times before the loop even starts, so only the last one actually is processed (emits a timeout() signal).

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mjsurette
                wrote on last edited by
                #9

                Thanks for the explanation. I'll refactor my test program.

                kshegunovK 1 Reply Last reply
                0
                • M mjsurette

                  Thanks for the explanation. I'll refactor my test program.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by kshegunov
                  #10

                  @mjsurette said:

                  In my case, I have updates to some data that comes in bursts of indeterminate size. I would like to wait until there have been no updates for 10ms or so before updating the screen so they can all be handled in a batch. This would be much more efficient.

                  If I understand correctly you want to block event processing for a given amount of time, which doesn't exactly play well with QTimer. You can have a timed wait on a locked synchronization primitive for that:

                  QSemaphore lock(0);
                  lock.tryAcquire(1, 1000);  // Wait 1s for the semaphore to be released.
                  

                  Note: If I've misunderstood, and you want not to block the event loop, then this is certainly not the solution.

                  PS. Another way for a blocking wait with event processing is by using a local event loop:

                  QObject * emitter; //< This is the object that will emit a signal when something happened and the wait should interrupt
                  
                  QEventLoop loop;
                  QObject::connect(emitter, SIGNAL(interruptWait()), &loop, SLOT(quit()));
                  loop.exec();  // Wait until interruptWait() has been called
                  

                  Kind regards.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0
                  • M mjsurette

                    I'm sorry, I thought that it was a common term. I guess not.

                    A watchdog timer is one that counts down from a preset value. Incoming events can reset it to its original value and thereby preventing it from timing out. If there is an absence of triggers for long enough it times out and triggers an action.

                    In my case, I have updates to some data that comes in bursts of indeterminate size. I would like to wait until there have been no updates for 10ms or so before updating the screen so they can all be handled in a batch. This would be much more efficient.

                    I originally thought that a QTimer in singleshot mode would do the trick, but you can only trigger it once and there is no way to restart it.

                    T Offline
                    T Offline
                    t3685
                    wrote on last edited by
                    #11

                    @mjsurette

                    Just making sure you keep in mind the inherit limitations of qtimer. Qt is not a real time system. See the documentation http://doc.qt.io/qt-5/qtimer.html for more information.

                    An alternative to batching by time, is to simply batch by size. It will be easier to write and the behaviour will probably be more consistent.

                    kshegunovK 1 Reply Last reply
                    1
                    • T t3685

                      @mjsurette

                      Just making sure you keep in mind the inherit limitations of qtimer. Qt is not a real time system. See the documentation http://doc.qt.io/qt-5/qtimer.html for more information.

                      An alternative to batching by time, is to simply batch by size. It will be easier to write and the behaviour will probably be more consistent.

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by kshegunov
                      #12

                      @t3685 said:

                      Qt is not a real time system. See the documentation http://doc.qt.io/qt-5/qtimer.html for more information.

                      Nothing to do with Qt. It's the limitation of the underlying OS you have to take in mind.

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      0
                      • M mjsurette

                        I'm sorry, I thought that it was a common term. I guess not.

                        A watchdog timer is one that counts down from a preset value. Incoming events can reset it to its original value and thereby preventing it from timing out. If there is an absence of triggers for long enough it times out and triggers an action.

                        In my case, I have updates to some data that comes in bursts of indeterminate size. I would like to wait until there have been no updates for 10ms or so before updating the screen so they can all be handled in a batch. This would be much more efficient.

                        I originally thought that a QTimer in singleshot mode would do the trick, but you can only trigger it once and there is no way to restart it.

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #13

                        @mjsurette
                        Just a note:
                        WatchDog -is- a common term, i just wanted to hear what your overall
                        goal was with it
                        as there are many way to program a watchdog. :)

                        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
                        • Unsolved