QTimer
1. 定时器QTimer
1.1 简介
在进行窗口程序的处理过程中, 经常要周期性的执行某些操作, 或者制作一些动画效果,看似比较复杂的问题使用定时器就可以完美的解决这些问题, Qt中提供了两种定时器方式:
- 一种是使用Qt中的事件处理函数;
- Qt中的定时器类 QTimer。
使用规则:只需创建一个QTimer类对象,然后调用其 start() 函数开启定时器,此后QTimer对象就会周期性的发出 timeout() 信号。
1.1 公共槽函数public slot function
// 构造函数
// 如果指定了父对象, 创建的堆内存可以自动析构
QTimer::QTimer(QObject *parent = nullptr);
// 设置定时器时间间隔为 msec 毫秒
// 默认值是0,一旦窗口系统事件队列中的所有事件都已经被处理完,一个时间间隔为0的QTimer就会触发
void QTimer::setInterval(int msec);
// 获取定时器的时间间隔, 返回值单位: 毫秒
int QTimer::interval() const;
// 根据指定的时间间隔启动或者重启定时器, 需要调用 setInterval() 设置时间间隔
[slot] void QTimer::start();
// 启动或重新启动定时器,超时间隔为msec毫秒。
[slot] void QTimer::start(int msec);
// 停止定时器。
[slot] void QTimer::stop();
// 设置定时器精度
/*
参数:
- Qt::PreciseTimer -> 精确的精度, 毫秒级
- Qt::CoarseTimer -> 粗糙的精度, 和1毫秒的误差在5%的范围内, 默认精度
- Qt::VeryCoarseTimer -> 非常粗糙的精度, 精度在1秒左右
*/
void QTimer::setTimerType(Qt::TimerType atype);
Qt::TimerType QTimer::timerType() const; // 获取当前定时器的精度
// 如果定时器正在运行,返回true; 否则返回false。
bool QTimer::isActive() const;
// 判断定时器是否只触发一次
bool QTimer::isSingleShot() const;
// 设置定时器是否只触发一次, 参数为true定时器只触发一次, 为false定时器重复触发, 默认为false
void QTimer::setSingleShot(bool singleShot);
1.2 信号函数
这个类的信号只有一个, 当定时器超时时,该信号就会被发射出来。给这个信号通过conect()关联一个槽函数, 就可以在槽函数中处理超时事件。
[signal] void QTimer::timeout();
1.3 成员函数
/*
功能: 在msec毫秒后发射一次信号, 并且只发射一次
参数:
- msec: 在msec毫秒后发射信号
- receiver: 接收信号的对象地址
- method: 槽函数地址
*/
[static] void QTimer::singleShot(
int msec, const QObject *receiver,
PointerToMemberFunction method);
1.4 实例操作
周期性定时器
// 创建定时器对象
QTimer* timer = new QTimer(this);
// 修改定时器对象的精度
timer->setTimerType(Qt::PreciseTimer);
// 按钮 timeBtn 的点击事件
// 点击按钮启动或者关闭定时器, 定时器启动, 周期性得到当前时间
connect(ui->timeBtn, &QPushButton::clicked, this, [=]()
{
// 启动定时器
if(timer->isActive()){
timer->stop();
ui->timeBtn->setText(u8"开始");
}
else
{
timer->start(1000);
ui->timeBtn->setText(u8"结束");
}
});
connect(timer, &QTimer::timeout, this, [=]()
{
QTime tm = QTime::currentTime();
// 格式化当前得到的系统时间
QString tmstr = tm.toString("hh:mm:ss.zzz");
// 设置要显示的时间
//ui->curTime->setText(tmstr);
qDebug() << tmstr;
});
一次性定时器
// 点击按钮 onceBtn 只发射一次信号
// 点击按钮一次, 发射一个信号, 得到某一个时间点的时间
connect(ui->onceBtn, &QPushButton::clicked, this, [=]()
{
// 获取2s以后的系统时间, 不创建定时器对象, 直接使用类的静态方法
QTimer::singleShot(2000, this, [=](){
QTime tm = QTime::currentTime();
// 格式化当前得到的系统时间
QString tmstr = tm.toString("hh:mm:ss.zzz");
// 设置要显示的时间
ui->onceTime->setText(tmstr);
});
});
2. 计时器类 QElapsedTimer
QElapsedTimer 是一个高精度计时器类,用于测量代码执行时间或实现精确的时间控制。
- 功能:提供纳秒级精度的计时器,用于测量时间间隔。
- 底层实现:基于平台特定的高精度计时器(如Windows的
QueryPerformanceCounter
、Linux的clock_gettime
)。 - 用途:性能测试、动画帧率控制、超时检测等。
精度与平台差异
- 精度:通常提供微秒级精度,但实际精度取决于操作系统和硬件。
- 平台差异:
- Windows:基于
QueryPerformanceCounter
,精度约1微秒。 - Linux:基于
clock_gettime(CLOCK_MONOTONIC)
,精度取决于内核。 - macOS:基于
mach_absolute_time
,精度较高。
- Windows:基于
2.1 常用方法
计时器操作
// 开始计时(重置计时器)
void start();
// 重新计时(返回自上次启动以来的时间,并重新启动)
qint64 restart();
// 获取自上次启动以来的毫秒数
qint64 elapsed() const;
// 获取自上次启动以来的微秒数
qint64 nsecsElapsed() const; // Qt5.8+ 支持
// 判断是否超过指定时间(毫秒)
bool hasExpired(qint64 timeout) const;
时间比较
// 计算两个时间点之间的差值(毫秒)
static qint64 elapsed(const QElapsedTimer &start, const QElapsedTimer &end);
2.2 使用场景
(1) 性能测试
#include <QElapsedTimer>
#include <QDebug>
void testPerformance() {
QElapsedTimer timer;
timer.start(); // 开始计时
// 执行需要测试的代码
for (int i = 0; i < 1000000; ++i) {
// ...
}
qint64 ms = timer.elapsed(); // 获取耗时(毫秒)
qDebug() << "代码执行耗时:" << ms << "毫秒";
}
(2) 超时检测
void waitForData() {
QElapsedTimer timer;
timer.start();
while (!dataReady()) {
// 检查是否超时(例如5000毫秒)
if (timer.hasExpired(5000)) {
qDebug() << "等待超时!";
return;
}
// 短暂休眠,避免CPU占用过高
QThread::msleep(10);
}
}
(3) 帧率控制(游戏/动画)
void updateAnimation() {
static QElapsedTimer frameTimer;
if (!frameTimer.isValid()) {
frameTimer.start(); // 首次调用时启动计时器
}
// 计算自上次更新以来的时间(毫秒)
qint64 msSinceLastFrame = frameTimer.restart();
// 根据时间间隔计算动画进度
float deltaTime = msSinceLastFrame / 1000.0f; // 转换为秒
updateScene(deltaTime); // 更新场景
// 控制帧率(例如60FPS)
qint64 frameTime = 1000 / 60; // 每帧时间(毫秒)
if (msSinceLastFrame < frameTime) {
QThread::msleep(frameTime - msSinceLastFrame);
}
}
检查精度
QElapsedTimer timer;
timer.start();
QThread::msleep(1); // 休眠1毫秒
qint64 elapsed = timer.nsecsElapsed();
qDebug() << "实际休眠时间:" << elapsed / 1000000.0 << "毫秒";
2.3 与其他时间类的对比
类名 | 精度 | 用途 |
---|---|---|
QElapsedTimer | 纳秒级 | 高精度计时、性能测试 |
QTime | 毫秒级 | 时钟时间显示、简单计时 |
QDateTime | 毫秒级 | 日期时间处理、时区转换 |
QTimer | 毫秒级 | 定时触发事件(非高精度) |
2.4 注意事项
-
计时器有效性:
if (!timer.isValid()) { timer.start(); // 确保计时器已启动 }
-
线程安全:
QElapsedTimer
本身是线程安全的,但在多线程环境中使用时需注意:// 错误示例:在不同线程中读取同一计时器 // 正确做法:每个线程维护自己的计时器 // 线程1 QElapsedTimer timer1; timer1.start(); // 线程2 QElapsedTimer timer2; timer2.start();
-
系统时间变化:
QElapsedTimer
使用单调时钟(monotonic clock),不受系统时间调整影响,适合测量持续时间。