Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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 http://www.bogotobogo.com/Qt/Qt5_QTimer.php

    Any suggestions as to why the timer is inconsistent?

    Thank you in advance.

    Hoyt

    mytimer.h

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

    myTimer.cpp

    #include "mytimer.h"
    #include <QDebug>
    
    MyTimer::MyTimer()
    {
        // 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

    Hi,

    What happens in the slot connected to ping ` ?



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

    Hoyt



  • 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



  • @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.

    Hoyt



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

    Hoyt


Log in to reply