Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Inconsistent / Inaccurate QTimer

Inconsistent / Inaccurate QTimer

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 2.1k Views 2 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.
  • H Offline
    H Offline
    Hoyt
    wrote on last edited by A Former User
    #1

    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

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

      Hi,

      What happens in the slot connected to ping ` ?

      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
      • H Offline
        H Offline
        Hoyt
        wrote on last edited by
        #3

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

        Hoyt

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          2
          • H Offline
            H Offline
            Hoyt
            wrote on last edited by
            #5

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

            Hoyt

            ? 1 Reply Last reply
            0
            • H Hoyt

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

              Hoyt

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

              @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 ;-)

              H 1 Reply Last reply
              2
              • ? A Former User

                @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 ;-)

                H Offline
                H Offline
                Hoyt
                wrote on last edited by
                #7

                @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

                H 1 Reply Last reply
                1
                • H Hoyt

                  @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

                  H Offline
                  H Offline
                  Hoyt
                  wrote on last edited by
                  #8

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

                  Hoyt

                  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