Qt 定时器 QTimer
2018-03-03 17:42
337 查看
Qt 定时器 QTimer
如果我们在某个类中要用到定时器的功能,可以选择两种方式:
1.如果此类继承于QObject,可以直接调用以下函数:int QObject::startTimer(int interval, Qt::TimerType timerType = Qt::CoarseTimer)此函数开启一个定时器,但只返回该定时器的编号,我们无法获取定时器对象(即使创建了定时器对象也不应该是QTimer类型的,因为,这是在QObject类中的成员函数,不应该出现子类QTimer的身影),所以也无法使用信号与槽机制,只能通过重载定时器事件处理函数:[virtual protected] void QObject::timerEvent(QTimerEvent *event)在timerEvent()函数中可以通过定时器Id来区分多个定时器,并分别处理;
关闭定时器可以在类中调用以下函数:void QObject::killTimer(int id)
此函数的参数为定时器的Id。
这里引用一下Qt Assistant中example: class MyObject : public QObject
{
Q_OBJECT
public:
MyObject(QObject *parent = 0);
protected:
void timerEvent(QTimerEvent *event);
};
MyObject::MyObject(QObject *parent)
: QObject(parent)
{
startTimer(50); // 50-millisecond timer
startTimer(1000); // 1-second timer
startTimer(60000); // 1-minute timer
}
void MyObject::timerEvent(QTimerEvent *event)
{
qDebug() << "Timer ID:" << event->timerId();
}
2.可以在类中添加一个QTimer类型的成员,这样我们就可以通过信号与槽机制类实现相应的功能,举例如下: QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);我们可以使用QTimer中的槽函数start()来开启定时器,设置发出timerEvent事件的时间间隔,或者不设置:void start(int msec);
void start();
使用stop()来停止定时器,设置时间间隔的第二个方式是调用setInterval()函数:void setInterval(int msec);
调用setTimerType()设置定时器的精度:void setTimerType(Qt::TimerType atype);
上述两种方法有两个函数都用到了Qt::TimerType类型的参数,下面详细介绍
int QObject::startTimer(int interval, Qt::TimerType timerType = Qt::CoarseTimer);void QTimer::setTimerType(Qt::TimerType atype);
Qt Assitant中的原文如下:
enum Qt::TimerTypeThe timer type indicates how accurate a timer can be.
Constant | Value | Description |
Qt::PreciseTimer | 0 | Precise timers try to keep millisecond accuracy |
Qt::CoarseTimer | 1 | Coarse timers try to keep accuracy within 5% of the desired interval |
Qt::VeryCoarseTimer | 2 | Very coarse timers only keep full second accuracy |
可见这个参数和定时器的精度有关,上述使用定时器的两种方式中其精度默认都是Qt::CoarseTimer,我大致解释一下上述含义:
在类UNIX系统中,Qt使用Qt::PreciseTimer来保持毫秒级精度(精度偏差最多1毫秒),对于Qt::CoarseTimer,实际间隔时间将会比配置间隔时间多或者少5%,以使得此定时器和其他想要在同一时刻或者临近时刻产生动作的定时器 相结合,那么cpu只需要一次,就可以唤醒这一堆定时器,从而减少cpu的唤醒次数和功耗,如果在使用Qt::PreciseTimer的时候,这一堆定时器可能需要CPU分别去唤醒,那么,功耗也会增加。(本人拙见,仅供参考,勿信以为真)
在windows平台,如果允许的话Qt将使用多媒体定时器来完成Qt::PreciseTimer的要求,使用常规Windows定时器来应对Qt::Coarse和Qt::VeryCoarseTimer。
在所有平台上,Qt::verycoarseTimer参数将会把interval参数的四舍五入成整秒的形式,即第三位全为0.
相关文章推荐
- QT里定时器QTimer的用法
- Qt的定时器QTimer
- Qt编程7:Qt定时器 QTimer使用
- Qt中使用定时器(可使用QObject::timerEvent定时执行,QTimer::singleShot可只触发一次)
- Qt5 定时器 QTimer 的测试
- Qt定时器QTimer的类定义与例子及代码详解
- 【引用】QT定时器QTimer
- Qt 定时器QTimer
- 请教如何在QT自定义线程类中使用QTimer定时器功能
- Qt中使用定时器(可使用QObject::timerEvent定时执行,QTimer::singleShot可只触发一次)
- Qt中使用定时器(可使用QObject::timerEvent定时执行,QTimer::singleShot可只触发一次)
- Qt 定时器 阻塞触发容易崩溃
- 1.关于QT中的Graphics绘图,定时器,动画,将窗口中的内容打印到图片上,打印机,打印预览
- QT::QTimerEvent
- QT用线程启动定时器
- QT软件开发-多窗口—定时器与直流电机控制
- 定时器之QTimer
- QT学习笔记 QTimer 与信号 槽
- QTimer 定时器
- QT 定时器demo