计时器

QObject 是所有 Qt 对象的基类,在 Qt 中提供基本计时器支持。采用 QObject::startTimer (),启动计时器以毫秒为单位的间隔作为自变量。函数返回唯一整数计时器 ID。计时器现在将按定期间隔被激发,直到明确调用 QObject::killTimer () 采用计时器 ID。

要使此机制工作,应用程序必须在事件循环中运行。启动事件循环采用 QApplication::exec ()。当计时器被激发时,应用程序发送 QTimerEvent ,并控制流离开事件循环,直到计时器事件被处理。这隐含计时器无法被激发,当应用程序忙于做某些事情时。换句话说:计时器精度从属应用程序粒度。

在多线程应用程序中,可以使用计时器机制在拥有事件循环的任何线程中。要从非 GUI 线程启动事件循环,使用 QThread::exec ()。Qt 使用对象的 线程倾向性 确定哪个线程将交付 QTimerEvent 。因此,必须启动和停止对象线程中的所有计时器;为另一线程中的对象启动计时器,是不可能的。

间隔值上限可以由按有符号整数指定的毫秒数确定 (在实践中,此周期刚刚超过 24 天)。精度从属底层操作系统。Windows 2000 拥有 15 毫秒精度;测试过的其它系统,可以处理 1 毫秒间隔。

计时器功能的主要 API 是 QTimer 。该类提供发射信号的常规计时器当计时器被激发时,且继承 QObject 因此,它能很好适应大多数 Qt 程序的所有权结构。平常使用方式是像这样:

    QTimer *timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &Foo::updateCaption);
    timer->start(1000);
					

The QTimer 对象被制作成子级化的 this 对象,因此当 this 对象被删除,计时器也会被删除。接下来,其 timeout () 信号将被连接到履行工作的槽,按 1000 毫秒值启动,指示它将每秒超时。

QTimer 还为单发计时器提供静态函数。例如:

    QTimer::singleShot(200, this, &Foo::updateCaption);
					

200 milliseconds (0.2 seconds) after this line of code is executed, the updateCaption() slot will be called.

For QTimer to work, you must have an event loop in your application; that is, you must call QCoreApplication::exec () somewhere. Timer events will be delivered only while the event loop is running.

在多线程应用程序中,可以使用 QTimer 在拥有事件循环的任何线程中。要从非 GUI 线程启动事件循环,使用 QThread::exec ()。Qt 使用计时器的 线程倾向性 确定哪个线程将发射 timeout () signal. Because of this, you must start and stop the timer in its thread; it is not possible to start a timer from another thread.

The 指针式时钟 范例展示如何使用 QTimer 以按定期间隔重新绘制 Widget。来自 AnalogClock 的实现:

AnalogClock::AnalogClock(QWidget *parent)
    : QWidget(parent)
{
    QTimer *timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, QOverload<>::of(&AnalogClock::update));
    timer->start(1000);
    ...
}
					

每秒, QTimer 会调用 QWidget::update () 槽以刷新时钟的显示。

If you already have a QObject subclass and want an easy optimization, you can use QBasicTimer 而不是 QTimer 。采用 QBasicTimer ,必须重实现 timerEvent () 在您的 QObject subclass and handle the timeout there. The Tetrix 范例展示如何使用 QBasicTimer .