QTimer 类

The QTimer 类提供重复 (和单发) 计时器。 更多...

头: #include <QTimer>
继承: QObject

特性

公共函数

QTimer (QObject * parent = 0)
~QTimer ()
int interval () const
bool isActive () const
bool isSingleShot () const
void setInterval (int msec )
void setSingleShot (bool singleShot )
int timerId () const

公共槽

void start (int msec )
void start ()
void stop ()

信号

void timeout ()

静态公共成员

void singleShot (int msec , QObject * receiver , const char * member )

重实现保护函数

virtual void timerEvent (QTimerEvent * e )

详细描述

The QTimer 类提供重复 (和单发) 计时器。

The QTimer 类为计时器提供高级编程接口。要使用它,创建 QTimer ,连接其 timeout () 信号到适当槽,并调用 start (). From then on it will emit the timeout () 信号按常量间隔。

一秒 (1000 毫秒) 计时器范例 (来自 指针式时钟 范例):

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    timer->start(1000);
					

从那时起, update() 槽被每秒调用。

可以将计时器设为仅超时一次,通过调用 setSingleShot (true)。也可以使用静态 QTimer::singleShot () 函数调用槽在指定间隔后:

    QTimer::singleShot(200, this, SLOT(updateCaption()));
					

在多线程应用程序中,可以使用 QTimer 在拥有事件循环的任何线程中。要从非 GUI 线程启动事件循环,使用 QThread::exec ()。Qt 使用计时器的 线程亲缘关系 确定哪个线程将发射 timeout() 信号。因此,必须在其线程中启动和停止计时器;从另一线程启动计时器,是不可能的。

作为特殊情况, QTimer 采用 0 超时将尽快超时,在已处理窗口系统事件队列中的所有事件后。可以使用这做繁重工作,当提供敏捷用户界面时:

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(processOneThing()));
    timer->start();
					

processOneThing() will from then on be called repeatedly. It should be written in such a way that it always returns quickly (typically after processing one data item) so that Qt can deliver events to widgets and stop the timer as soon as it has done all its work. This is the traditional way of implementing heavy work in GUI applications; multithreading is now becoming available on more and more platforms, and we expect that zero-millisecond QTimers will gradually be replaced by QThread

精度和计时器分辨率

Timers will never time out earlier than the specified timeout value and they are not guaranteed to time out at the exact value specified. In many situations, they may time out late by a period of time that depends on the accuracy of the system timers.

计时器的准确性取决于底层操作系统和硬件。大多数平台支持 1 毫秒的分辨率,虽然计时器的准确性在许多真实世界状况下不会等于此分辨率。

If Qt is unable to deliver the requested number of timer clicks, it will silently discard some.

替代 QTimer

替代使用 QTimer 是调用 QObject::startTimer () 为对象并重实现 QObject::timerEvent () 事件处理程序在类中 (必须继承 QObject )。缺点是 timerEvent () 不支持如单次计时器 (或信号) 的高级特征。

Another alternative to using QTimer is to use QBasicTimer 。通常不那么麻烦相比使用 QObject::startTimer () 直接。见 计时器 了解所有 3 种途径的概述。

某些操作系统限制可能使用的计时器数;Qt 试着绕过这些局限性。

另请参阅 QBasicTimer , QTimerEvent , QObject::timerEvent (), 计时器 , 指针式时钟范例 ,和 摆动范例 .

特性文档编制

active : const bool

This boolean property is true if the timer is running; otherwise false.

该特性在 Qt 4.3 引入。

访问函数:

bool isActive () const

interval : int

This property holds the timeout interval in milliseconds.

此特性的默认值为 0。 QTimer 采用 0 超时间隔会尽快超时,在已处理窗口系统事件队列中的所有事件后。

设置活动计时器的间隔会改变其 timerId ().

访问函数:

int interval () const
void setInterval (int msec )

另请参阅 singleShot .

singleShot : bool

This property holds whether the timer is a single-shot timer.

单发计时器仅激发一次,非单发计时器被激发每隔 interval 毫秒。

访问函数:

bool isSingleShot () const
void setSingleShot (bool singleShot )

另请参阅 interval and singleShot ().

成员函数文档编制

QTimer:: QTimer ( QObject * parent = 0)

构造计时器采用给定 parent .

QTimer:: ~QTimer ()

销毁计时器。

[static] void QTimer:: singleShot ( int msec , QObject * receiver , const char * member )

此静态函数调用槽,在给定时间间隔后。

使用此函数非常方便,因为不需要麻烦采用 timerEvent 或创建本地 QTimer 对象。

范例:

#include <QApplication>
#include <QTimer>
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QTimer::singleShot(600000, &app, SLOT(quit()));
    ...
    return app.exec();
}
					

此范例程序在 10 分钟 (600,000 毫秒) 后自动终止。

The receiver 是接收对象而 member 是槽。时间间隔为 msec 毫秒。

注意: 此函数是 可重入 .

另请参阅 setSingleShot () 和 start ().

[slot] void QTimer:: start ( int msec )

启动 (或重启) 计时器采用超时间隔 msec 毫秒。

若计时器已经在运行,它会被 stopped 并重启。

singleShot 为 true,计时器将仅被激活一次。

[slot] void QTimer:: start ()

此函数重载 start ().

启动 (或重启) 计时器采用指定超时在 interval .

若计时器已经在运行,它会被 stopped 并重启。

singleShot 为 true,计时器将仅被激活一次。

[slot] void QTimer:: stop ()

停止计时器。

另请参阅 start ().

[signal] void QTimer:: timeout ()

此信号被发射当计时器超时。

另请参阅 interval , start (),和 stop ().

[virtual protected] void QTimer:: timerEvent ( QTimerEvent * e )

重实现自 QObject::timerEvent ().

int QTimer:: timerId () const

返回计时器的 ID,若计时器正在运行;否则返回 -1。