Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange accuracy of QTimer with Windows 8

Tags:

c++

timer

qt

I'm using Qt 5.2.1 with Windows 8.1.

I'm facing a strange behavior regarding QTimer accuracy on Windows 8.1.

I'm starting a timer which is supposed to timeout every 20 milliseconds. To check that, I also use a QTime object to measure the elapsed time between two ticks.

If I start the timer with an interval of 20ms, I measure an effective interval of 30ms. If I start the timer with an interval of 19ms, I measure an effective interval of 19ms !

Here is a small project producing the problem :

main.cpp :

#include "mainfrm.h"
#include <QApplication>

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);

    MainFrm w(20);
    w.setMinimumSize(400,50);
    w.show();

    MainFrm w2(19);
    w2.setMinimumSize(400,50);
    w2.show();

    return a.exec();
}

mainfrm.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QLabel>
#include <QTime>
#include <QTimer>

class MainFrm : public QLabel {
    Q_OBJECT

public:
    explicit MainFrm(int TimerInterval = 20, QWidget *parent = 0);

private:
    QTime m_LastUpdateTime;
    QTimer m_TickTimer;

    unsigned int m_ElapsedSum;
    unsigned int m_TickCount;

private slots:
    void onTick();
};
#endif // WIDGET_H

mainfrm.cpp

#include "mainfrm.h"

MainFrm::MainFrm(int TimerInterval, QWidget *parent) : QLabel(parent) {

    m_ElapsedSum = 0;
    m_TickCount = 0;

    m_TickTimer.setInterval(TimerInterval);
    connect(&m_TickTimer, SIGNAL(timeout()), this, SLOT(onTick()));

    m_TickTimer.start();
}

void MainFrm::onTick() {

    int ElapsedTime = m_LastUpdateTime.elapsed();
    m_LastUpdateTime.start();

    if (ElapsedTime < 1) ElapsedTime = 1;

    m_ElapsedSum += ElapsedTime;
    m_TickCount++;

    this->setText(QString("FPS : %1, Elapsed : %2ms, Mean : %3ms, Remaining time : %4")
                                      .arg(1000/ElapsedTime)
                                      .arg(ElapsedTime)
                                      .arg(m_ElapsedSum/m_TickCount)
                                      .arg(m_TickTimer.remainingTime()));

}

This code opens two windows (overlapping), the first one with a timer with an interval of 19ms. The accuracy is fine. The second one is using a timer with an interval of 20ms. The accuracy is bad.

I compiled the same code on Linux (Ubuntu 12.04) and everything is fine. The accuracy is fine for both intervals.

I really don't understand why I get this problem on Windows. I know Windows cannot guarantee a high accuracy (and I don't need it), but why is it able to cope with an interval of 19ms and not 20ms ?

Is there a bug in my code ?

Here is a zip-file containing a Qt project : testtimer.zip

like image 244
Jérôme Avatar asked Mar 21 '23 03:03

Jérôme


1 Answers

Have you looked at the QTimer precision setting?

http://qt-project.org/doc/qt-5/qtimer.html#timerType-prop

It defaults to coarse precision.

like image 104
Brett Stottlemyer Avatar answered Mar 28 '23 00:03

Brett Stottlemyer