Does QTimer keep track of the time after it ends?

I would like to know if the QTimer will keep counting as soon as it expires.

For example, let's say I have a QTimer that is played every 500ms. Let's say when this particular QTimer expires, my program ends up in some function I did (not in an event loop), and this program takes 10ms for my function, in an event loop. Does this mean the next time the QTimer will be at 1010ms?

If so, is there a way to get around this?

Thank.

Edit: Timer results

+3


source to share


2 answers


I've done some tests using multiple timers, but with timerEvent

( QObject::setTimer()

).

The point is, if you have multiple timers that get in the way at some point (check at exactly the same time), all your code doStuffThatTake10ms

will be "enqueued" ... But the absolute precision of the timers should stay higher time. Here's some code to try.

#ifndef MULTITIMER_H
#define MULTITIMER_H

#include <QObject>
#include <QTime>

class MultiTimer : public QObject
{
    Q_OBJECT
public:
    explicit MultiTimer(QObject *parent = 0);
    void timerEvent(QTimerEvent *event);

private:
    int timerId[4];
    int interval[4];
    int count[4];
    QTime absoluteTimer;
};

#endif // MULTITIMER_H

      

.Cpp implementation

#include "MultiTimer.h"
#include <QTimerEvent>
#include <QTime>
#include <QDebug>

MultiTimer::MultiTimer(QObject *parent) :
    QObject(parent)
{
    interval[0] = 500;
    interval[1] = 1000;
    interval[2] = 1500;
    interval[3] = 2000;

    for( int i = 0; i < 4; i++)
        timerId[i] = startTimer(interval[i]);

    for( int i = 0; i < 4; i++)
        count[i] = 0;

    absoluteTimer.start();
}

void MultiTimer::timerEvent(QTimerEvent *event)
{
    int id = -1;
    for( int i = 0; i < 4; i++){
        if( event->timerId() == timerId[i]){
            id = i;
            count[id]++;
            break;
        }
    }

    if( id != -1) {
        qDebug() << "timer" << id
                 << "interval" << interval[id]
                 << "count" << count[id]
                 << "total" << count[id]*interval[id]
                 << "reference" << absoluteTimer.elapsed();

        usleep(10000);
    }
}

      



Try to create an object a = QApllication()

and MultiTimer

and and run a.exec()

.

Result after 1 minute on linux

timer 1 interval 1000 count 59 total 59000 reference 59010 
timer 0 interval 500 count 119 total 59500 reference 59500 
timer 0 interval 500 count 120 total 60000 reference 60000 
timer 1 interval 1000 count 60 total 60000 reference 60010 
timer 3 interval 2000 count 30 total 60000 reference 60021 
timer 2 interval 1500 count 40 total 60000 reference 60031 
timer 0 interval 500 count 121 total 60500 reference 60500 

      

As you can see, the 121st tick is timer 0

turned on in time at 60500ms

... but 60000ms

all timers are faced with deferred execution.

+1


source


When you place a Qtimer with a delay d

, it fires a signal timeout()

after that time period has elapsed and as soon as all events in the window system's event queue have been processed.

So, if your program is spending all its time outside the event loop, then that's okay. Anyway, in Qt or not, setting the timeout to t ms

really means waking me up when no less has passedt ms

. You cannot guarantee accuracy.



ps: If you are worried about a few ms

, then you should worry about time granularity.

+2


source







All Articles