Inconsistent / Inaccurate QTimer

  • I am using QTimer to generate a signal every second. The timer works well for a minute or two. However, after that time, the signal is not generated for up to 10 seconds.

    The majority of the below timer code was taken from

    Any suggestions as to why the timer is inconsistent?

    Thank you in advance.



    #ifndef MYTIMER_H
    #define MYTIMER_H
    #include <QTimer>
    #include "animations.h"
    extern Animations animations;
    class MyTimer : public QObject
        QTimer *timer;
    public slots:
        void MyTimerSlot();
        void ping(int);
    #endif // MYTIMER_H


    #include "mytimer.h"
    #include <QDebug>
        // create a timer
        timer = new QTimer(this);
        // setup signal and slot
        connect(timer, SIGNAL(timeout()),
              this, SLOT(MyTimerSlot()));
        timer->start(1000);  // msec
    void MyTimer::MyTimerSlot()
        qDebug() << "Timer...";
        emit ping(1);

    Edit: Added code tags -- @Wieland

  • Lifetime Qt Champion


    What happens in the slot connected to ping ` ?

  • The same inconsistent behavior occurs if the slot is connected to ping.


  • The code for the timer is totally fine. The problem must be somewhere else. And as the code you posted doesn't contain anything more than the timer and that "ping" signal, said signal, resp. the slot / slots connected to it must be blamed.

  • If my computer (a Mac) is running other applications with a high CPU usage, could those applications block the timer for multiple seconds?


  • @Hoyt Yes, if each core is under 100% load for several seconds and all other processes have the highest priority. In other words: Very unlikely. I blame your code ;-)

  • @Wieland

    My computer is currently rendering an animation that is using all cores at close to 100%. i will try my code again when the rendering is complete.

    Thank you for your assistance.


  • I have verified that the code works properly when the animation is not running.


Log in to reply