QTimer Class
QTimer 提供了重复定时器和单次定时器. More...
头文件: | #include <QTimer> |
qmake: | QT += core |
基类: | QObject |
属性
|
|
- 1 个属性继承自 QObject
公有函数
QTimer(QObject *parent = nullptr) | |
virtual | ~QTimer() |
QMetaObject::Connection | callOnTimeout(Functor slot, Qt::ConnectionType connectionType = Qt::AutoConnection) |
QMetaObject::Connection | callOnTimeout(const QObject *context, Functor slot, Qt::ConnectionType connectionType = Qt::AutoConnection) |
QMetaObject::Connection | callOnTimeout(const QObject *receiver, MemberFunction *slot, Qt::ConnectionType connectionType = Qt::AutoConnection) |
int | interval() const |
std::chrono::milliseconds | intervalAsDuration() const |
bool | isActive() const |
bool | isSingleShot() const |
int | remainingTime() const |
std::chrono::milliseconds | remainingTimeAsDuration() const |
void | setInterval(int msec) |
void | setInterval(std::chrono::milliseconds value) |
void | setSingleShot(bool singleShot) |
void | setTimerType(Qt::TimerType atype) |
void | start(std::chrono::milliseconds msec) |
int | timerId() const |
Qt::TimerType | timerType() const |
- 32 个公有函数继承自 QObject
公有槽函数
- 1 个公有槽函数继承自 QObject
信号
void | timeout() |
- 2 个信号继承自 QObject
静态公有成员
void | singleShot(int msec, const QObject *receiver, const char *member) |
void | singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, const char *member) |
void | singleShot(int msec, const QObject *receiver, PointerToMemberFunction method) |
void | singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, PointerToMemberFunction method) |
void | singleShot(int msec, Functor functor) |
void | singleShot(int msec, Qt::TimerType timerType, Functor functor) |
void | singleShot(int msec, const QObject *context, Functor functor) |
void | singleShot(int msec, Qt::TimerType timerType, const QObject *context, Functor functor) |
void | singleShot(std::chrono::milliseconds msec, const QObject *receiver, const char *member) |
void | singleShot(std::chrono::milliseconds msec, Qt::TimerType timerType, const QObject *receiver, const char *member) |
- 11 个静态公有成员继承自 QObject
重新实现的受保护函数
virtual void | timerEvent(QTimerEvent *e) |
- 9 个受保护的函数继承自 QObject
详细描述
QTimer 为定时器提供了高级编程接口. 要使用它,需创建一个 QTimer, 将 timeout() 信号连接到相应的槽函数, 然后调用 start() 函数. 然后, QTimer 将以固定时间间隔发出 timeout() 信号.
1秒 (1000 毫秒) 定时器示例 (摘自 Analog Clock ):
QTimer *timer = new QTimer(this); connect(timer, &QTimer::timeout, this, QOverload<>::of(&AnalogClock::update)); timer->start(1000);
之后, update()
槽函数每秒都会被调用.
你可以调用 setSingleShot(true) 仅触发一次定时器. 你也可以使用静态 QTimer::singleShot() 函数在特定的时间内调用一个槽函数:
QTimer::singleShot(200, this, &Foo::updateCaption);
在多线程应用, 你在任何一个有事件循环的线程使用 QTimer. 使用 QThread::exec(), 开启一个非 GUI 线程的事件循环. Qt 使用 thread affinity 定义哪一个线程会发出 timeout() 信号. 因此, 你必须在指定线程内开启和关闭定时器; 你不能从另一个线程开启定时器.
在特殊案例里, 窗口系统的事件队列中的所有事件都已处理后,超时为0的定时器将立即处理. 在提供快速的用户界面时, 这可以用来完成繁重的工作:
QTimer *timer = new QTimer(this); connect(timer, &QTimer::timeout, this, &Foo::processOneThing); timer->start();
之后, processOneThing()
将会被重复调用. 这个函数应该以始终快速返回 (通常在处理一个数据项之后) 的方式编写. 因此, Qt 一处理完定时器上所有工作后, 就能够分发事件提供给用户接口和停止定时器. 对于 GUI 应用程序来说, 这是典型的方法实现繁重工作. 但如今越来越多应用了多线程,我们期待0毫秒的定时器对象会逐渐被 QThread 取代.
精度和时间分辨率
定时器的精度依赖底层操作系统和硬件. 大多数定时器支持1毫秒的分辨率, 尽管在许多实际情况下定时器的精度将不等于该分辨率.
定时器的精度依靠 timer type. 对于 Qt::PreciseTimer, QTimer 会尽量保持一毫秒的精度. 精确的定时器也永远不会比预期的超时.
对于 Qt::CoarseTimer 和 Qt::VeryCoarseTimer 类型, QTimer 可能会早于我们预期中唤醒, 在这些类型的范围内: 5% 的间隔 Qt::CoarseTimer 和 500 毫秒 Qt::VeryCoarseTimer.
如果操作系统繁忙或是不能提供所需精度, 所有的时间类型都可能会晚于我们期待的. 在这种超时溢出的情况下, 即使多个超时已过期, Qt 也仅发出一次 timeout() , 然后将恢复原始间隔.
QTimer 的其他方法
另一种使用 QTimer 的方法是调用 QObject::startTimer(), 然后重新实现实现 QObject::timerEvent() 事件, 这个类必须继承 QObject. timerEvent() 的缺点是不支持单次定时器或信号的高级功能.
还有一种方法是 QBasicTimer. 通常, 与直接使用 QObject::startTimer() 相比, 它不那么麻烦. 有关这三种方法的概述, 参见 Timers.
有些操作系统可能会限制定时器的使用数量, Qt尽量工作在这些范围之内.
另见 QBasicTimer, QTimerEvent, QObject::timerEvent(), Timers, Analog Clock Example, Wiggly Example.
Property Documentation
active : const bool
This boolean property is true
if the timer is running; otherwise false.
This property was introduced in Qt 4.3.
Access functions:
bool | isActive() const |
interval : int
This property holds the timeout interval in milliseconds
The default value for this property is 0. A QTimer with a timeout interval of 0 will time out as soon as all the events in the window system's event queue have been processed.
Setting the interval of an active timer changes its timerId().
Access functions:
int | interval() const |
void | setInterval(int msec) |
void | setInterval(std::chrono::milliseconds value) |
See also singleShot.
remainingTime : const int
This property holds the remaining time in milliseconds
Returns the timer's remaining value in milliseconds left until the timeout. If the timer is inactive, the returned value will be -1. If the timer is overdue, the returned value will be 0.
This property was introduced in Qt 5.0.
Access functions:
int | remainingTime() const |
See also interval.
singleShot : bool
This property holds whether the timer is a single-shot timer
A single-shot timer fires only once, non-single-shot timers fire every interval milliseconds.
The default value for this property is false
.
Access functions:
bool | isSingleShot() const |
void | setSingleShot(bool singleShot) |
See also interval and singleShot().
timerType : Qt::TimerType
controls the accuracy of the timer
The default value for this property is Qt::CoarseTimer
.
Access functions:
Qt::TimerType | timerType() const |
void | setTimerType(Qt::TimerType atype) |
See also Qt::TimerType.
Member Function Documentation
QTimer::QTimer(QObject *parent = nullptr)
Constructs a timer with the given parent.
[slot]
void QTimer::start()
This function overloads start().
Starts or restarts the timer with the timeout specified in interval.
If the timer is already running, it will be stopped and restarted.
If singleShot is true, the timer will be activated only once.
[slot]
void QTimer::start(int msec)
Starts or restarts the timer with a timeout interval of msec milliseconds.
If the timer is already running, it will be stopped and restarted.
If singleShot is true, the timer will be activated only once.
[slot]
void QTimer::stop()
Stops the timer.
See also start().
[signal]
void QTimer::timeout()
This signal is emitted when the timer times out.
Note: This is a private signal. It can be used in signal connections but cannot be emitted by the user.
See also interval, start(), and stop().
[virtual]
QTimer::~QTimer()
Destroys the timer.
template <typename Functor> QMetaObject::Connection QTimer::callOnTimeout(Functor slot, Qt::ConnectionType connectionType = Qt::AutoConnection)
This is an overloaded function.
Creates a connection of type connectionType from the timeout() signal to slot, and returns a handle to the connection.
This method is provided for convenience. It's equivalent to calling QObject::connect(timer, &QTimer::timeout, timer, slot, connectionType)
.
This function was introduced in Qt 5.12.
See also QObject::connect() and timeout().
template <typename Functor> QMetaObject::Connection QTimer::callOnTimeout(const QObject *context, Functor slot, Qt::ConnectionType connectionType = Qt::AutoConnection)
This function overloads callOnTimeout().
Creates a connection from the timeout() signal to slot to be placed in a specific event loop of context, and returns a handle to the connection.
This method is provided for convenience. It's equivalent to calling QObject::connect(timer, &QTimer::timeout, context, slot, connectionType)
.
This function was introduced in Qt 5.12.
See also QObject::connect() and timeout().
template <typename MemberFunction> QMetaObject::Connection QTimer::callOnTimeout(const QObject *receiver, MemberFunction *slot, Qt::ConnectionType connectionType = Qt::AutoConnection)
This function overloads callOnTimeout().
Creates a connection from the timeout() signal to the slot in the receiver object. Returns a handle to the connection.
This method is provided for convenience. It's equivalent to calling QObject::connect(timer, &QTimer::timeout, receiver, slot, connectionType)
.
This function was introduced in Qt 5.12.
See also QObject::connect() and timeout().
std::chrono::milliseconds QTimer::intervalAsDuration() const
Returns the interval of this timer as a std::chrono::milliseconds
object.
This function was introduced in Qt 5.8.
See also interval.
bool QTimer::isActive() const
Returns true
if the timer is running (pending); otherwise returns false.
Note: Getter function for property active.
std::chrono::milliseconds QTimer::remainingTimeAsDuration() const
Returns the time remaining in this timer object as a std::chrono::milliseconds
object. If this timer is due or overdue, the returned value is std::chrono::milliseconds::zero()
. If the remaining time could not be found or the timer is not active, this function returns a negative duration.
This function was introduced in Qt 5.8.
See also remainingTime().
[static]
void QTimer::singleShot(int msec, const QObject *receiver, const char *member)
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 or create a local QTimer object.
Example:
#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).
The receiver is the receiving object and the member is the slot. The time interval is msec milliseconds.
Note: This function is reentrant.
See also setSingleShot() and start().
[static]
void QTimer::singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, const char *member)
This is an overloaded function.
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 or create a local QTimer object.
The receiver is the receiving object and the member is the slot. The time interval is msec milliseconds. The timerType affects the accuracy of the timer.
Note: This function is reentrant.
See also start().
[static]
template <typename PointerToMemberFunction> void QTimer::singleShot(int msec, const QObject *receiver, PointerToMemberFunction method)
This is an overloaded function.
This static function calls a member function of a QObject after a given time interval.
It is very convenient to use this function because you do not need to bother with a timerEvent or create a local QTimer object.
The receiver is the receiving object and the method is the member function. The time interval is msec milliseconds.
If receiver is destroyed before the interval occurs, the method will not be called. The function will be run in the thread of receiver. The receiver's thread must have a running Qt event loop.
Note: This function is reentrant.
This function was introduced in Qt 5.4.
See also start().
[static]
template <typename PointerToMemberFunction> void QTimer::singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, PointerToMemberFunction method)
This is an overloaded function.
This static function calls a member function of a QObject after a given time interval.
It is very convenient to use this function because you do not need to bother with a timerEvent or create a local QTimer object.
The receiver is the receiving object and the method is the member function. The time interval is msec milliseconds. The timerType affects the accuracy of the timer.
If receiver is destroyed before the interval occurs, the method will not be called. The function will be run in the thread of receiver. The receiver's thread must have a running Qt event loop.
Note: This function is reentrant.
This function was introduced in Qt 5.4.
See also start().
[static]
template <typename Functor> void QTimer::singleShot(int msec, Functor functor)
This is an overloaded function.
This static function calls functor after a given time interval.
It is very convenient to use this function because you do not need to bother with a timerEvent or create a local QTimer object.
The time interval is msec milliseconds.
Note: This function is reentrant.
This function was introduced in Qt 5.4.
See also start().
[static]
template <typename Functor> void QTimer::singleShot(int msec, Qt::TimerType timerType, Functor functor)
This is an overloaded function.
This static function calls functor after a given time interval.
It is very convenient to use this function because you do not need to bother with a timerEvent or create a local QTimer object.
The time interval is msec milliseconds. The timerType affects the accuracy of the timer.
Note: This function is reentrant.
This function was introduced in Qt 5.4.
See also start().
[static]
template <typename Functor, int> void QTimer::singleShot(int msec, const QObject *context, Functor functor)
This is an overloaded function.
This static function calls functor after a given time interval.
It is very convenient to use this function because you do not need to bother with a timerEvent or create a local QTimer object.
The time interval is msec milliseconds.
If context is destroyed before the interval occurs, the method will not be called. The function will be run in the thread of context. The context's thread must have a running Qt event loop.
Note: This function is reentrant.
This function was introduced in Qt 5.4.
See also start().
[static]
template <typename Functor, int> void QTimer::singleShot(int msec, Qt::TimerType timerType, const QObject *context, Functor functor)
This is an overloaded function.
This static function calls functor after a given time interval.
It is very convenient to use this function because you do not need to bother with a timerEvent or create a local QTimer object.
The time interval is msec milliseconds. The timerType affects the accuracy of the timer.
If context is destroyed before the interval occurs, the method will not be called. The function will be run in the thread of context. The context's thread must have a running Qt event loop.
Note: This function is reentrant.
This function was introduced in Qt 5.4.
See also start().
[static]
void QTimer::singleShot(std::chrono::milliseconds msec, const QObject *receiver, const char *member)
This is an overloaded function.
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 or create a local QTimer object.
The receiver is the receiving object and the member is the slot. The time interval is given in the duration object msec.
Note: This function is reentrant.
This function was introduced in Qt 5.8.
See also start().
[static]
void QTimer::singleShot(std::chrono::milliseconds msec, Qt::TimerType timerType, const QObject *receiver, const char *member)
This is an overloaded function.
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 or create a local QTimer object.
The receiver is the receiving object and the member is the slot. The time interval is given in the duration object msec. The timerType affects the accuracy of the timer.
Note: This function is reentrant.
This function was introduced in Qt 5.8.
See also start().
void QTimer::start(std::chrono::milliseconds msec)
This is an overloaded function.
Starts or restarts the timer with a timeout of duration msec milliseconds.
If the timer is already running, it will be stopped and restarted.
If singleShot is true, the timer will be activated only once.
This function was introduced in Qt 5.8.
[override virtual protected]
void QTimer::timerEvent(QTimerEvent *e)
Reimplements: QObject::timerEvent(QTimerEvent *event).
int QTimer::timerId() const
Returns the ID of the timer if the timer is running; otherwise returns -1.