I am using Qt 5.2.1 with Windows 8.1.
I came across strange behavior regarding accuracy QTimerin Windows 8.1.
I start a timer that should expire every 20 milliseconds. To test this, I also use the QTime object to measure elapsed time between two ticks.
If I start the timer with an interval of 20 ms, I measure the effective interval of 30 ms. If I start the timer at 19 ms intervals, I measure the effective interval at 19 ms!
Here is a small project creating a 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
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 with a timer with an interval of 19 ms. Accuracy is good. The second is a timer with an interval of 20 ms. Accuracy is bad.
Linux (Ubuntu 12.04), . .
, Windows. , Windows ( ), 19 , 20 ?
?
zip , Qt: testtimer.zip