c++ - Qt精密定时器计时精度

标签 c++ qt qt5

我写了一个简单的演示来检查单次定时器的准确性。以下是我的代码:

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 not accurate at all? ),QTimer 可能根本不那么准确。

最佳答案

如果你想要 2 个事件之间的时间,你必须使用 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
...

关于c++ - Qt精密定时器计时精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55168709/

相关文章:

c++ - 使用命令行参数打开特定主题的 .HLP 文件

c++ - Clang vs G++ 左值到右值转换

c++ - 如何在 Qt 应用程序中处理纯正则表达式结果?

c++ - 在 QTextEdit 中放置图像的几种方法

c++ - 从 Qt 中的错误中恢复

c++ - 字节数组和 QString 转换的问题

c++ - 将 C++ 结构成员从非常量转换为常量

c++ - 如何动态设置类型?

qt - 如何将 QQuickView 的内容打印为 PDF?

c++ - 各种小部件中 Qt 应用程序超链接中的 Cursor apper odd