QTimer 类提供重复 (和一次性) 计时器。 更多...
继承 QObject .
QTimer 类提供重复 (和一次性) 计时器。
The 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, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);
从那时起, update() slot is called every second.
可以把计时器设为仅超时一次,通过调用 setSingleShot(true). You can also use the static QTimer.singleShot () function to call a slot after a specified interval:
QTimer.singleShot(200, this, SLOT(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 QThread.exec (). Qt uses the timer's 线程倾向性 去确定哪个线程将发射 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.
As a special case, a QTimer with a timeout of 0 will time out as soon as all the events in the window system's event queue have been processed. This can be used to do heavy work while providing a snappy user interface:
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 s.
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.
The accuracy of timers depends on the underlying operating system and hardware. Most platforms support a resolution of 1 millisecond, though the accuracy of the timer will not equal this resolution in many real-world situations.
If Qt is unable to deliver the requested number of timer clicks, it will silently discard some.
使用 QTimer 的替代是调用 QObject.startTimer () for your object and reimplement the QObject.timerEvent () event handler in your class (which must inherit QObject )。缺点是 timerEvent () does not support such high-level features as single-shot timers or signals.
Another alternative to using QTimer is to use QBasicTimer . It is typically less cumbersome than using QObject.startTimer () directly. See 计时器 for an overview of all three approaches.
Some operating systems limit the number of timers that may be used; Qt tries to work around these limitations.
parent argument, if not None, causes self to be owned by Qt instead of PyQt.
构造计时器采用给定 parent .
This static function calls a slot after a given time interval.
It is very convenient to use this function because you do not need to bother with a 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(); }
This sample program automatically terminates after 10 minutes (600,000 milliseconds).
receiver 是接收对象而 member 是槽。时间间隔为 msec 毫秒。
注意: 此函数是 可重入 .
另请参阅 setSingleShot () 和 start ().
This method is also a Qt slot with the C++ signature void start(int) .
启动 (或重启) 计时器采用超时间隔 msec 毫秒。
若计时器已经在运行,它会被 stopped 并重启。
若 singleShot is true, the timer will be activated only once.
This method is also a Qt slot with the C++ signature void start() .
此函数重载 start ().
启动 (或重启) 计时器采用指定超时在 interval .
若计时器已经在运行,它会被 stopped 并重启。
若 singleShot is true, the timer will be activated only once.
This method is also a Qt slot with the C++ signature void stop() .
停止计时器。
另请参阅 start ().
重实现自 QObject.timerEvent ().
Returns the ID of the timer if the timer is running; otherwise returns -1.
This is the default overload of this signal.
此信号被发射当计时器超时。