QT精确的计时器计时精度

Qt precise timer timing accuracy

本文关键字:精度 计时器 QT      更新时间:2023-10-16

我编写一个简单的演示以检查单次计时器的精度。以下是我的代码:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //two singleshot timers  
    QTimer::singleShot(1000,Qt::PreciseTimer,this,SLOT(myslot1()));
    QTimer::singleShot(10000,Qt::PreciseTimer,this,SLOT(myslot2()));
    //metrics of timing accuraccy
    t = 0;
    timer = new QTimer();
    timer->setInterval(100);
    timer->setTimerType(Qt::PreciseTimer);
    connect(timer,SIGNAL(timeout()),this,SLOT(timerhandler()),Qt::DirectConnection);
    timer->start();
}
void MainWindow::myslot1()
{
    qDebug()<<"myslot1 called: t="<<100*t;
    ui->lcdNumber1->display(1);
}
void MainWindow::myslot2()
{
    qDebug()<<"myslot2 called: t="<<100*t;
    ui->lcdNumber2->display(2);
}
void MainWindow::timerhandler()
{
    t++;
    ui->lcdNumber->display(t*100);
}

运行这些代码给出:

myslot1 called: t= 900
myslot2 called: t= 9900

在我看来,对于确切的计时器,时间误差约为100毫秒。我是对的吗?我设置了timer的时间间隔导致的明显错误?根据某些职位(QTimer根本不准确吗?),QTimer可能根本不太准确。

如果您想要两个事件之间的时间,则必须使用 QElapsedTimer

示例:

#include <QtCore>
class Foo: public QObject
{
    Q_OBJECT
public:
    Foo(QObject *parent=nullptr):
        QObject(parent),
        timer(new QTimer())
    {
        QTimer::singleShot(1000,Qt::PreciseTimer,this, &Foo::myslot1);
        QTimer::singleShot(10000,Qt::PreciseTimer,this, &Foo::myslot2);
        timer->setInterval(100);
        timer->setTimerType(Qt::PreciseTimer);
        connect(timer,&QTimer::timeout,this,&Foo::timerhandler,Qt::DirectConnection);
        timer->start();
        timer_measure.start();
    }
private:
    Q_SLOT void myslot1(){
        qDebug()<< __PRETTY_FUNCTION__ << timer_measure.elapsed();
    }
    Q_SLOT void myslot2(){
        qDebug()<< __PRETTY_FUNCTION__ << timer_measure.elapsed();
    }
    void timerhandler()
    {
        qDebug()<< __PRETTY_FUNCTION__ << timer_measure.elapsed();
    }
    QElapsedTimer timer_measure;
    QTimer *timer;
};
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    Foo foo;
    return a.exec();
}
#include "main.moc"

输出:

void Foo::timerhandler() 100
void Foo::timerhandler() 200
void Foo::timerhandler() 301
void Foo::timerhandler() 400
void Foo::timerhandler() 500
void Foo::timerhandler() 600
void Foo::timerhandler() 700
void Foo::timerhandler() 800
void Foo::timerhandler() 900
void Foo::myslot1() 1000
void Foo::timerhandler() 1001
void Foo::timerhandler() 1100
void Foo::timerhandler() 1200
void Foo::timerhandler() 1300
void Foo::timerhandler() 1401
void Foo::timerhandler() 1500
void Foo::timerhandler() 1600
void Foo::timerhandler() 1700
void Foo::timerhandler() 1801
void Foo::timerhandler() 1900
void Foo::timerhandler() 2000
void Foo::timerhandler() 2100
void Foo::timerhandler() 2201
void Foo::timerhandler() 2300
void Foo::timerhandler() 2400
void Foo::timerhandler() 2500
void Foo::timerhandler() 2600
void Foo::timerhandler() 2700
void Foo::timerhandler() 2800
void Foo::timerhandler() 2901
void Foo::timerhandler() 3000
void Foo::timerhandler() 3100
void Foo::timerhandler() 3200
void Foo::timerhandler() 3301
void Foo::timerhandler() 3400
void Foo::timerhandler() 3500
void Foo::timerhandler() 3600
void Foo::timerhandler() 3701
void Foo::timerhandler() 3800
void Foo::timerhandler() 3900
void Foo::timerhandler() 4000
void Foo::timerhandler() 4101
void Foo::timerhandler() 4200
void Foo::timerhandler() 4300
void Foo::timerhandler() 4400
void Foo::timerhandler() 4501
void Foo::timerhandler() 4600
void Foo::timerhandler() 4700
void Foo::timerhandler() 4800
void Foo::timerhandler() 4901
void Foo::timerhandler() 5000
void Foo::timerhandler() 5100
void Foo::timerhandler() 5200
void Foo::timerhandler() 5300
void Foo::timerhandler() 5400
void Foo::timerhandler() 5500
...