QTimer 类提供重复和单次计时器。
Header: | #include <QTimer> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
Inherits: | < QObject |
- 所有成员的列表,包括继承的成员
- QTimer 类是事件类 Event Classes 的一部分。
【详细说明】
QTimer 类为计时器提供高级编程接口。使用时请先创建一个 QTimer,将其 timeout() 信号连接到相应的槽函数,然后调用 start(),此时,它将以恒定的间隔发出 timeout() 信号。
示例:一秒(1000 毫秒)计时器:
QTimer *timer = new QTimer(this); connect(timer, &QTimer::timeout, this, QOverload<>::of(&AnalogClock::update)); timer->start(1000);
从此时,程序每秒调用一次槽函数。用户可以通过 setSingleShot(true) , 将计时器设置为仅超时一次。您还可以使用 static 函数 QTimer::singleShot() 在指定的时间间隔后调用槽:QTimer::singleShot(200, this, &Foo::updateCaption);
在多线程应用程序中,可以在任何具有事件循环的线程中使用 QTimer。
要从非 GUI 线程启动事件循环,请使用 QThread::exec()。
Qt使用线程亲和关系来确定哪个线程将发出timeout() 信号。因此,必须在其线程中启动和停止计时器,无法从另一个线程启动计时器。
作为特殊情况,“零计时器”也可用于完成一些工作,同时提供快速的用户界面:
QTimer *timer = new QTimer(this); connect(timer, &QTimer::timeout, this, &Foo::processOneThing); timer->start();
引时,处理过程processOneThing() 将被反复调用并快速返回(通常是在处理一个数据项之后),这样Qt就可以将事件传递到用户界面,并在它完成所有工作后停止计时器。
这是GUI应用程序中实现繁重工作的传统方法,但是随着多线程在越来越多的平台上可用,我们期望零毫秒QTimer对象将逐渐被QThreads所取代。
★ 定时器精度
计时器的准确性取决于底层操作系统和硬件。大多数平台都支持 1 毫秒的分辨率,尽管在许多实际情况下,计时器的精度不会等于此分辨率。
精度还取决于计时器类型 Qt::PreciseTimer,QTimer 将尝试将精度保持在 1 毫秒。
对于 Qt::CoarseTimer 和 Qt::VeryCoarseTimer 类型,QTimer可能会比预期更早唤醒。
★ QTimer 的替代品
使用 QTimer 的另一种方法是为您的对象调用 QObject::startTimer() ,并在您的类中重新实现 QObject::timerEvent() 事件处理程序(必须继承 )。timerEvent() 的缺点是不支持单次定时器或信号等高级功能。
另一种选择是 QBasicTimer,它通常比直接使用 QObject::startTimer() 更省事。
某些操作系统限制了可以使用的计时器数量,Qt 将试图绕过这些限制。
参见: QBasicTimer, QTimerEvent, QObject::timerEvent(), Timers, and Analog Clock.
【属性】
active : bool | 如果计时器正在运行为真;否则为假。 |
interval : int | 此属性保存以毫秒为单位的超时间隔。默认值为0。 |
remainingTime : const int | 此属性保存剩余时间(以毫秒为单位) 返回超时前计时器的剩余值(毫秒)。如果计时器处于非活动状态,返回值将为-1。如果计时器超时,返回值将为0。 |
singleShot : bool | 此属性保存计时器是否为单次计时器。 单次计时器仅触发一次,默认值为 |
timerType : Qt::TimerType | 控制计时器的精确度。 |
【公有函数】
QTimer(QObject *parent = nullptr) | |
virtual | ~QTimer() |
QBindable<bool> | |
返回计时器是否正在运行。 | |
QBindable<int> | |
返回计时器超时间隔。 | |
QBindable<bool> | |
返回是否是单次计时器。 | |
QBindable<Qt::TimerType> | |
返回计时器精度枚举。 | |
QMetaObject::Connection | callOnTimeout(Functor &&slot) |
创建从计时器的 timeout()信号到指定slot的连接。返回连接的句柄。 这相当于调用:QObject::connect(timer, &QTimer::timeout, timer, slot, Qt::DirectConnection); | |
QMetaObject::Connection | callOnTimeout(const QObject *context, Functor &&slot, Qt::ConnectionType connectionType = Qt::AutoConnection) |
重载。创建一个从timeout()信号到指定slot的连接,并放在特定的事件循环context中,返回该连接的句柄。 相当于调用: | |
int | interval() const |
返回interval属性保存的以毫秒为单位的超时间隔。 | |
std::chrono::milliseconds | intervalAsDuration() const |
以标准库std::chrono::milliseconds形式, 返回此计时器的时间间隔。 | |
bool | isActive() const |
如果计时器正在运行(挂起),则返回true否则返回false。 | |
bool | isSingleShot() const |
是否为单次计时器 | |
int | remainingTime() const |
返回剩余时间。 | |
std::chrono::milliseconds | remainingTimeAsDuration() const |
以标准库std::chrono::milliseconds形式, 返回计时器对象中的剩余时间。 | |
void | setInterval(int msec) |
设置以毫秒为单位的超时间隔msec。 | |
void | setInterval(std::chrono::milliseconds value) |
以标准库std::chrono::milliseconds形式, 设置以毫秒为单位的超时间隔msec。 | |
void | setSingleShot(bool singleShot) |
设置为单次计时器。 | |
void | setTimerType(Qt::TimerType atype) |
以Qt::TimerType枚举值设置计时器。 | |
void | start(std::chrono::milliseconds msec) |
以启动或重新启动计时器。如果计时器已经在运行,它将stopped 并重新启动。如果singleShot为真,计时器将仅被激活一次。这相当于:
注意:零计时器会让事件循环忙碌,可能导致麻烦和UI的高度不稳定行为。 | |
int | timerId() const |
如果计时器正在运行,则返回计时器的ID;否则返回-1。 | |
Qt::TimerType | timerType() const |
返回当前计时器精度的枚举值。
|
【公有槽函数】
void | start(int msec) | 以msec毫秒的时间隔启动或重新启动计时器。如果计时器已经在运行,它将stopped 并重新启动。如果singleShot为true,计时器将仅激活一次。这相当于: 注意:用一个零计时器来保持事件循环繁忙,一定会导致麻烦和UI的高度不稳定行为。 |
void | start() | 重载。按照间隔interval中指定的超时时间启动或重新启动计时器。 【其余同上】 |
void | stop() | 停止计时器。 |
【信号】
void | timeout() | 这个信号在计时器每次时间结束时发出。 |
【静态公有成员】
void | singleShot(Duration msec, const QObject *context, Functor &&functor) |
void | singleShot(Duration msec, Qt::TimerType timerType, const QObject *context, Functor &&functor) |
void | singleShot(Duration msec, Functor &&functor) |
void | singleShot(Duration msec, Qt::TimerType timerType, Functor &&functor) |
上面几个静态函数在msec毫秒后调用。 此函数非常方便,因为不需要创建本地对象 QTimer或timerEvent 。 如果指定context,则仅当对象在间隔发生之前未被销毁时,才会调用 。 msec参数可以是int 或 std::chrono::milliseconds 值。 注意:此函数是 reentrant。 |
void | singleShot(int msec, const QObject *receiver, const char *member) |
这个静态函数在给定的时间间隔后调用一个槽函数。 使用这个函数非常方便,因为您不需要使用timerEvent或者创建一个本地的QTimer对象。 该示例程序在10分钟(600000毫秒)后自动终止。 receiver是接收对象,member 就是槽函数。 注意:此函数是 reentrant。 | |
void | singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, const char *member) |
重载。与上面不同的是提供了一个timerType,用来指定计时精度。 | |
void | singleShot(std::chrono::milliseconds msec, const QObject *receiver, const char *member) |
重载。与上面不同的是给出时间的方式 std::chrono::milliseconds。 | |
void | singleShot(std::chrono::milliseconds msec, Qt::TimerType timerType, const QObject *receiver, const char *member) |
重载。与上面不同的是提供了一个timerType,用来指定计时精度。 |
【重新实现的保护成员】
virtual void | timerEvent(QTimerEvent *e) override 重新实现的QObject::timerEvent(QTimerEvent *event),用于接收对象的计时器事件 |