1.QTimer构造
QTimer的构造提供了一个默认,因此即可以指定父对象,也可以缺省。
2.QTimer使用
#include <QTimer>
QTimer *timer = new QTimer(this);
//QTimer *timer = new QTimer; 二者都可以
//在多线程情况下,QTimer 只能由创建线程调用,不能跨线程调用
//创建完定时器,需要连接信号槽,QTimer的信号是timeout
//信号槽
connect(timer,&QTimer::timeout,this,&mainwindow::get);
//开启定时器
timer->start(1000); //单位是毫秒
//暂停定时器
timer->stop();
//Qt有个timerEvent事件。
//也可以让定时器执行一次,start(1000)是每隔一秒执行一次定时器操作,是循环操作
QTimer::singleShot(200, this, SLOT(get()));
3.QTimer成员函数
bool isActive() const
如果定时器正在运行,返回true,否则返回false。int remainingTime() const
返回定时器的剩余时间(毫秒为单位),直到超时。如果定时器不活跃,返回值是-1。如果定时器过期,返回值为0。
void setInterval(int msec)
设置超时间隔(毫秒为单位)。
默认值是0,这时,一旦窗口系统事件队列中的所有事件都已经被处理完,一个时间间隔为0的QTimer就会触发。void setSingleShot(bool singleShot)
设置定时器是否为单次触发。
单次触发定时器只触发一次,非单次的话,则每过一个时间间隔都会触发。void setTimerType(Qt::TimerType atype)
设置定时器的准确性。默认值是Qt::CoarseTimer。int timerId() const
如果定时器正在运行,返回定时器的ID,否则返回-1。void start(int msec)
启动或重新启动一个超时时间间隔为毫秒的定时器。如果定时器正在运行,它将被停止和重新启动。如果singleShot为true,定时器将只激活一次。
void start()
同上,重载了start()。void stop()
停止定时器。