内容表

上一话题

QTimeZone

下一话题

QTimerEvent

QTimer

QTimer class provides repetitive and single-shot timers. 更多

Inheritance diagram of PySide2.QtCore.QTimer

概要

函数

静态函数

详细描述

QTimer class provides a high-level programming interface for timers. To use it, create a QTimer , connect its timeout() signal to the appropriate slots, and call start() . From then on, it will emit the timeout() signal at constant intervals.

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

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

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

可以把计时器设为仅超时一次,通过调用 setSingleShot (true)。也可以使用静态 singleShot() function to call a slot after a specified interval:

QTimer.singleShot(200, self.updateCaption)
											

In multithreaded applications, you can use QTimer in any thread that has an event loop. To start an event loop from a non-GUI thread, use exec() . Qt uses the timer’s thread affinity 去确定哪个线程将发射 timeout() 信号。因此,必须在其线程中启动 停止计时器;从另一线程启动计时器是不可能的。

As a special case, a QTimer with a timeout of 0 will time out as soon as possible, though the ordering between zero timers and other sources of events is unspecified. Zero timers can be used to do some work while still providing a snappy user interface:

timer = QTimer(self)
timer.timeout.connect(self.processOneThing)
timer.start()
											

从那时起, processOneThing() will 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 the user interface 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, but as multithreading is nowadays becoming available on more and more platforms, we expect that zero-millisecond QTimer objects will gradually be replaced by QThread s.

精度和计时器分辨率

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

精度还取决于 timer type . For PreciseTimer , QTimer will try to keep the accuracy at 1 millisecond. Precise timers will also never time out earlier than expected.

For CoarseTimer and VeryCoarseTimer types, QTimer may wake up earlier than expected, within the margins for those types: 5% of the interval for CoarseTimer 和 500 ms 为 VeryCoarseTimer .

所有计时器类型的超时均可能晚于预期,若系统繁忙 (或无法提供要求的精度)。在这种超时超限情况下,Qt 会发射 timeout() only once, even if multiple timeouts have expired, and then will resume the original interval.

替代 QTimer

替代使用 QTimer is to call startTimer() for your object and reimplement the timerEvent() event handler in your class (which must inherit QObject )。缺点是 timerEvent() does not support such high-level features as single-shot timers or signals.

另一替代是 QBasicTimer . It is typically less cumbersome than using startTimer() directly. See 计时器 了解所有 3 种途径的概述。

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

class QTimer ( [ parent=None ] )
param parent

QObject

构造计时器采用给定 parent .

PySide2.QtCore.QTimer. interval ( )
返回类型

int

另请参阅

setInterval()

PySide2.QtCore.QTimer. isActive ( )
返回类型

bool

返回 true 若计时器正在运行 (待决);否则返回 false。

PySide2.QtCore.QTimer. isSingleShot ( )
返回类型

bool

PySide2.QtCore.QTimer. remainingTime ( )
返回类型

int

PySide2.QtCore.QTimer. setInterval ( msec )
参数

msec int

另请参阅

interval()

PySide2.QtCore.QTimer. setSingleShot ( singleShot )
参数

singleShot bool

另请参阅

singleShot()

PySide2.QtCore.QTimer. setTimerType ( atype )
参数

atype TimerType

另请参阅

timerType()

static PySide2.QtCore.QTimer. singleShot ( arg__1 , arg__2 )
参数
  • arg__1 int

  • arg__2 PyCallable

static PySide2.QtCore.QTimer. singleShot ( msec , timerType , receiver , member )
参数
  • msec int

  • timerType TimerType

  • receiver QObject

  • member – str

这是重载函数。

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

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

receiver 是接收对象而 member 是槽。时间间隔为 msec 毫秒。 timerType 影响计时器精度。

另请参阅

start()

static PySide2.QtCore.QTimer. singleShot ( msec , receiver , member )
参数
  • msec int

  • receiver QObject

  • member – str

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

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

范例:

from PySide2.QtCore import QApplication, QTimer
def main():
    app = QApplication([])
    QTimer.singleShot(600000, app, SLOT('quit()'))
    ...
    return app.exec_()
												

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

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

PySide2.QtCore.QTimer. start ( )

This function overloads .

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

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

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

PySide2.QtCore.QTimer. start ( msec )
参数

msec int

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

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

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

PySide2.QtCore.QTimer. stop ( )

停止计时器。

另请参阅

start()

PySide2.QtCore.QTimer. timerId ( )
返回类型

int

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

PySide2.QtCore.QTimer. timerType ( )
返回类型

TimerType

另请参阅

setTimerType()