QTimer Class
The QTimer class provides repetitive and single-shot timers. More…
| Header | #include <QTimer> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
| Inherits | QObject |
List of all members, including inherited members
Public Functions
| QTimer(QObject *parent = nullptr) | |
| ~QTimer() | |
| bool | isActive() const |
| int | timerId() const |
| Qt::TimerId | id() const |
| void | setInterval(int msec) |
| void | setInterval(std::chrono::milliseconds interval) |
| int | interval() const |
| std::chrono::milliseconds | intervalAsDuration() const |
| int | remainingTime() const |
| std::chrono::milliseconds | remainingTimeAsDuration() const |
| void | setSingleShot(bool singleShot) |
| bool | isSingleShot() const |
| void | setTimerType(Qt::TimerType type) |
| Qt::TimerType | timerType() const |
| QMetaObject::Connection | callOnTimeout(Func &&functor, Qt::ConnectionType type = Qt::AutoConnection) |
| QMetaObject::Connection | callOnTimeout(const RecvT *receiver, Func &&slot, Qt::ConnectionType type = Qt::AutoConnection) |
Public Slots
Signals
| void | timeout() |
Static Public Members
| void | singleShot(std::chrono::milliseconds interval, Func &&functor) |
| void | singleShot(std::chrono::milliseconds interval, const RecvT *context, Func &&functor) |
| void | singleShot(int msec, Func &&functor) |
| void | singleShot(int msec, const RecvT *context, Func &&functor) |
Detailed Description
A QTimer emits timeout() each time its interval elapses. Connect that signal to whatever should run, then call start():
QTimer timer;
QObject::connect(&timer, &QTimer::timeout, [] { poll(); });
timer.start(250);
Timers run on the event loop, so timeout() is only emitted while a QCoreApplication or QEventLoop is executing. An interval is a minimum delay, not a guarantee: a busy loop delivers the signal late.
A timer normally repeats until stop() is called. Call setSingleShot(true) to have it fire once, or use the static singleShot() functions when no timer object is needed.
See also Signals and Slots.
Differences from Qt
Intervals are stored as an int number of milliseconds. The timer type is stored but does not change the precision, and interval properties cannot be bound. A timer must be started and stopped from the thread it lives in; the thread needs a running event loop.
Member Function Documentation
QTimer::QTimer(QObject *parent = nullptr)
Constructs a timer with the given parent. The timer is not started.
QTimer::~QTimer()
Destroys the timer. A running timer stops without emitting timeout().
template <typename Func>QMetaObject::Connection QTimer::callOnTimeout(Func &&functor, Qt::ConnectionType type = Qt::AutoConnection)
Connects timeout() to functor, which is called each time the timer fires, with the connection type. Returns the connection.
This function was introduced in 5.12.
See also timeout().
template <typename RecvT, typename Func>QMetaObject::Connection QTimer::callOnTimeout(const RecvT *receiver, Func &&slot, Qt::ConnectionType type = Qt::AutoConnection)
This is an overloaded function.
Connects timeout() to slot, a member function of receiver or a functor run in the thread of receiver, with the connection type. The connection is removed when receiver is destroyed.
This function was introduced in 5.12.
Qt::TimerId QTimer::id() const
Returns the identifier of the running timer as a Qt::TimerId, or Qt::TimerId::Invalid if it is not running.
This function was introduced in 6.8.
See also timerId().
int QTimer::interval() const
Returns the timeout interval in milliseconds. The default is 0.
See also setInterval() and intervalAsDuration().
std::chrono::milliseconds QTimer::intervalAsDuration() const
Returns the timeout interval as a std::chrono::milliseconds value.
See also interval().
bool QTimer::isActive() const
Returns true if the timer is running; otherwise returns false.
bool QTimer::isSingleShot() const
Returns true if the timer stops after its first timeout. The default is false.
See also setSingleShot().
int QTimer::remainingTime() const
Returns the number of milliseconds until the next timeout, 0 if it is overdue, or -1 if the timer is not running.
This function was introduced in 5.0.
See also remainingTimeAsDuration() and interval().
std::chrono::milliseconds QTimer::remainingTimeAsDuration() const
Returns the time until the next timeout as a std::chrono::milliseconds value: zero if it is overdue, or -1 ms if the timer is not running.
This function was introduced in 5.8.
See also remainingTime().
void QTimer::setInterval(int msec)
Sets the timeout interval to msec milliseconds.
If the timer is running it restarts, so the next timeout is a full interval from now.
See also interval() and start().
void QTimer::setInterval(std::chrono::milliseconds interval)
This is an overloaded function.
Sets the timeout interval to interval.
void QTimer::setSingleShot(bool singleShot)
Sets whether the timer stops after its first timeout. It does if singleShot is true; otherwise it repeats.
See also isSingleShot().
void QTimer::setTimerType(Qt::TimerType type)
Sets the timer type to type. Nt records it, but every type has the millisecond precision of the event dispatcher.
See also timerType().
template <typename Func> void QTimer::singleShot(std::chrono::milliseconds interval, Func &&functor)
This is an overloaded function.
Calls functor once, after interval.
template <typename RecvT, typename Func> void QTimer::singleShot(std::chrono::milliseconds interval, const RecvT *context, Func &&functor)
This is an overloaded function.
Calls functor once, after interval, unless context is destroyed first.
template <typename Func> void QTimer::singleShot(int msec, Func &&functor)
Calls functor once, msec milliseconds from now.
The call is made from the event loop. It cannot be cancelled; use the context overload if the target may be destroyed first.
template <typename RecvT, typename Func> void QTimer::singleShot(int msec, const RecvT *context, Func &&functor)
This is an overloaded function.
Calls functor once, msec milliseconds from now, unless context is destroyed first. The functor runs in the thread that context lives in, so this also runs it in another thread, as long as that thread has an event loop.
void QTimer::start()
Starts the timer with the current interval, or restarts it if it is already running. With a negative interval the timer does not start.
See also stop() and isActive().
void QTimer::start(int msec)
This is an overloaded function.
Sets the interval to msec milliseconds and starts the timer.
void QTimer::start(std::chrono::milliseconds interval)
This is an overloaded function.
Sets the interval to interval and starts the timer.
void QTimer::stop()
Stops the timer. Calling this on a stopped timer has no effect.
See also start() and isActive().
void QTimer::timeout()
This signal is emitted each time the timer's interval elapses. A single-shot timer is already stopped when the signal is emitted.
See also start() and setInterval().
int QTimer::timerId() const
Returns the identifier of the running timer, or 0 if it is not running.
Qt::TimerType QTimer::timerType() const
Returns the timer type. The default is Qt::CoarseTimer.
See also setTimerType().