Qt中定时器介绍和使用

Qt中定时器介绍和使用

Qt中和定时器相关的类或方法有:QElapsedTimerQBasicTimerQObject::startTimer()QTimer

QElapsedTimer

QElapsedTimer 常用于快速计算两个事件之间经过的时间,如计算某个耗时操作耗时时长。

常用函数介绍

void start():启动定时器,通常在要计算耗时操作前启动;

qint64 elapsed() const:返回自调用start()函数开始,经历的毫秒数

bool hasExpired(qint64 timeout) const:QElapsedTimer 是否超过 timeout指定的时间,timeout可为-1,表示永不超时;超时返回true.

void invalidate():使QElapsedTimer无效

bool isValid() const:判断定时器是否有效

qint64 msecsSinceReference() const

qint64 msecsTo(const QElapsedTimer &other) const:返回此QElapsedTimer 和其它QElapsedTimer之间的毫秒数;如果other QElapsedTimer 在此QElapsedTimer之前启动,返回值为负,否则返回为正;

qint64 nsecsElapsed() const:返回QElapsedTimer 启动后的纳秒数

qint64 restart():重新启动定时器,并返回上次启动后经过的毫秒数

qint64 secsTo(const QElapsedTimer &other) const:返回此QElapsedTimer和其他QElapsetTimer之间的秒数。如果other在此对象之前启动,则返回值将为负。如果稍后启动,返回值将为正。

QElapsedTimer的使用实例

QElapsedTimer一般使 start() 配合elapsed()函数配套使用,或者start()配合restart()函数配套使用,用以计算操作的耗时;常用于查找耗时操作

复制代码
   //计算操作耗时
   void testOpElapsed(){
       //声明对象并使能
       QElapsedTimer timer;
       timer.start();
       //耗时操作
       doLongTimerOp();
       //获取耗时操作所使用时间
       qint64 elapTime = timer.elapsed();
   }

//用于在特定时间片内执行特定操作或每个操作需要执行指定时间
void execOpForTime(int ms){

     QElapsedTimer timer;
     timer.start();
     
     while(!timer.hasExpired(ms)){
        //要执行的操作
        slowOp();
     }

}

QBasicTimer

QBasicTimer是一个低层级的类;QBasicTimer是一个重复计时器,除非调用stop()函数,否则它将发送后续计时器事件(QTimeEvent)。在调用其start()函数时需要指定定时器间隔和指向QObject子类的指针,定时器超时后,它将向 QObject 子类发送定时器事件。可以使用 stop() 在任何时候停止定时器。 isActive() 返回 true 表示定时器正在运行;可以使用 timerId() 检索定时器的 ID。与 QTimer 不同,它不会发射信号,开销更小,适合对性能敏感的场景。

常用函数介绍

bool isActive() const

  • 定时器是否在运行,true:运行

void start(std::chrono::milliseconds duration, QObject *object)

  • 启动定时器。
  • msec:定时周期(毫秒)
  • obj:定时器事件的接收者(必须继承 QObject,且实现 timerEvent())内部实际调用 QObject::startTimer()。

void start(std::chrono::milliseconds duration, Qt::TimerType timerType, QObject *obj)

  • 启动定时器

void stop()

  • 停止定时器

void swap(QBasicTimer &other)

  • 与other 定时器进行交互,此操作很快不会失败

int timerId() const

  • 获取定时器的Id

使用实例:

在QBasicTimer在调用start()函数时,需要指定一个QObject子类指针,用于接收定时事件;所以需要在QObject的子类中需要重载void QObject::timerEvent(QTimerEvent *event) 处理定时事件

复制代码
class MyObject : public QObject
{
   Q_OBJECT

public:
   MyObject() {
       timer.start(1000, this); // 1秒触发一次
   }

protected:
   void timerEvent(QTimerEvent *event) override {
       if (event->timerId() == timer.timerId()) {
           qDebug() << "Timer triggered!";
       }
   }

private:
   QBasicTimer timer;
};

QObject::startTimer()

QObject::startTimer()启动一个定时器,并返回一个定时器的ID或者如果没有启动定时器返回0;会周期的产生定时事件(QTimerEvent)直到调用killTimer()函数;如果间隔周期设置为0,会一直产生定时事件(QTimerEvent)。需要重载QObject::timeEvent()函数处理定时事件

使用实例

复制代码
    class MyObject : public QObject
{
    Q_OBJECT

public:
    MyObject(QObject *parent = nullptr);

protected:
    void timerEvent(QTimerEvent *event) override;
};

MyObject::MyObject(QObject *parent)
    : QObject(parent)
{
    startTimer(50);     // 50-millisecond timer
    startTimer(1000);   // 1-second timer
    startTimer(60000);  // 1-minute timer

    using namespace std::chrono;
    startTimer(milliseconds(50));
    startTimer(seconds(1));
    startTimer(minutes(1));

    // since C++14 we can use std::chrono::duration literals, e.g.:
    startTimer(100ms);
    startTimer(5s);
    startTimer(2min);
    startTimer(1h);
}

void MyObject::timerEvent(QTimerEvent *event)
{
    qDebug() << "Timer ID:" << event->timerId();
}

QTimer

QTimer 是一个高级编程接口,创建QTimer对象,设置定时器间隔,提供槽函数,使用connect()函数连接QTimer的timeout()信号,便可周期的处理定时事件。QTimer还提供了很多便捷接口用于处理单次定时事件。

使用实例

常规使用方式:定义对象,设置间隔,连接timeout()信号,启动定时器,停止定时器。

复制代码
int main(int argc, char *argv[])
{
   QCoreApplication a(argc, argv);

   QTimer timer;
   QObject::connect(&timer, &QTimer::timeout, [](){
       qDebug() << "Timer tick!";
   });

   timer.start(1000); // 每 1 秒触发一次
   return a.exec();
}

使用QTimer::singleShot()函数,做单次定时操作。注意在使用QTimer::singleShot()如果绑定类成员函数做处理函数时,需要留意类的生命周期,如果在定时器超时前,类已经析构情况。

复制代码
   QTimer::singleShot(2000, [](){
   qDebug() << "Triggered after 2 seconds, only once!";
});