def
interval
()
def
isActive
()
def
isSingleShot
()
def
remainingTime
()
def
setInterval
(msec)
def
setSingleShot
(singleShot)
def
setTimerType
(atype)
def
timerId
()
def
timerType
()
def
singleShot
(arg__1, arg__2)
def
singleShot
(msec, receiver, member)
def
singleShot
(msec, timerType, receiver, member)
QTimerclass provides a high-level programming interface for timers. To use it, create aQTimer, connect itstimeout()signal to the appropriate slots, and callstart(). From then on, it will emit thetimeout()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
QTimerin any thread that has an event loop. To start an event loop from a non-GUI thread, useexec(). Qt uses the timer’sthread affinity去确定哪个线程将发射timeout()信号。因此,必须在其线程中启动 停止计时器;从另一线程启动计时器是不可能的。As a special case, a
QTimerwith 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-millisecondQTimerobjects will gradually be replaced byQThreads.
计时器的准确性取决于底层操作系统和硬件。大多数平台支持 1 毫秒的分辨率,虽然计时器的准确性在许多真实世界情况下不会等于此分辨率。
精度还取决于
timer type. ForPreciseTimer,QTimerwill try to keep the accuracy at 1 millisecond. Precise timers will also never time out earlier than expected.For
CoarseTimerandVeryCoarseTimertypes,QTimermay wake up earlier than expected, within the margins for those types: 5% of the interval forCoarseTimer和 500 ms 为VeryCoarseTimer.所有计时器类型的超时均可能晚于预期,若系统繁忙 (或无法提供要求的精度)。在这种超时超限情况下,Qt 会发射
timeout()only once, even if multiple timeouts have expired, and then will resume the original interval.
替代使用
QTimeris to callstartTimer()for your object and reimplement thetimerEvent()event handler in your class (which must inheritQObject)。缺点是timerEvent()does not support such high-level features as single-shot timers or signals.另一替代是
QBasicTimer. It is typically less cumbersome than usingstartTimer()directly. See 计时器 了解所有 3 种途径的概述。某些操作系统限制可能使用的计时器数;Qt 试着绕过这些局限性。
PySide2.QtCore.QTimer.
interval
(
)
¶
int
另请参阅
PySide2.QtCore.QTimer.
isActive
(
)
¶
bool
返回
true
若计时器正在运行 (待决);否则返回 false。
PySide2.QtCore.QTimer.
isSingleShot
(
)
¶
bool
PySide2.QtCore.QTimer.
remainingTime
(
)
¶
int
PySide2.QtCore.QTimer.
setInterval
(
msec
)
¶
msec
–
int
另请参阅
PySide2.QtCore.QTimer.
setSingleShot
(
singleShot
)
¶
singleShot
–
bool
另请参阅
PySide2.QtCore.QTimer.
setTimerType
(
atype
)
¶
atype
–
TimerType
另请参阅
PySide2.QtCore.QTimer.
singleShot
(
arg__1
,
arg__2
)
¶
arg__1
–
int
arg__2
–
PyCallable
PySide2.QtCore.QTimer.
singleShot
(
msec
,
timerType
,
receiver
,
member
)
¶
msec
–
int
timerType
–
TimerType
receiver
–
QObject
member – str
这是重载函数。
此静态函数调用槽,在给定时间间隔后。
使用此函数非常方便,因为不需要麻烦采用
timerEvent
或创建本地
QTimer
对象。
receiver
是接收对象而
member
是槽。时间间隔为
msec
毫秒。
timerType
影响计时器精度。
另请参阅
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.
timerId
(
)
¶
int
返回计时器的 ID,若计时器正在运行;否则返回 -1。
PySide2.QtCore.QTimer.
timerType
(
)
¶
TimerType
另请参阅