QtBase
v6.3.1
|
The QTimer class provides repetitive and single-shot timers. More...
#include <qtimer.h>
Public Slots | |
void | start (int msec) |
void | start () |
void | stop () |
Public Slots inherited from QObject | |
void | deleteLater () |
Signals | |
void | timeout (QPrivateSignal) |
Signals inherited from QObject | |
void | destroyed (QObject *=nullptr) |
void | objectNameChanged (const QString &objectName, QPrivateSignal) |
Static Public Member Functions | |
static void | singleShot (int msec, const QObject *receiver, const char *member) |
static void | singleShot (int msec, Qt::TimerType timerType, const QObject *receiver, const char *member) |
template<typename Duration , typename Func1 > | |
static void | singleShot (Duration interval, const typename QtPrivate::FunctionPointer< Func1 >::Object *receiver, Func1 slot) |
template<typename Duration , typename Func1 > | |
static void | singleShot (Duration interval, Qt::TimerType timerType, const typename QtPrivate::FunctionPointer< Func1 >::Object *receiver, Func1 slot) |
template<typename Duration , typename Func1 > | |
static std::enable_if<!QtPrivate::FunctionPointer< Func1 >::IsPointerToMemberFunction &&!std::is_same< const char *, Func1 >::value, void >::type | singleShot (Duration interval, Func1 slot) |
template<typename Duration , typename Func1 > | |
static std::enable_if<!QtPrivate::FunctionPointer< Func1 >::IsPointerToMemberFunction &&!std::is_same< const char *, Func1 >::value, void >::type | singleShot (Duration interval, Qt::TimerType timerType, Func1 slot) |
template<typename Duration , typename Func1 > | |
static std::enable_if<!QtPrivate::FunctionPointer< Func1 >::IsPointerToMemberFunction &&!std::is_same< const char *, Func1 >::value, void >::type | singleShot (Duration interval, const QObject *context, Func1 slot) |
template<typename Duration , typename Func1 > | |
static std::enable_if<!QtPrivate::FunctionPointer< Func1 >::IsPointerToMemberFunction &&!std::is_same< const char *, Func1 >::value, void >::type | singleShot (Duration interval, Qt::TimerType timerType, const QObject *context, Func1 slot) |
Static Public Member Functions inherited from QObject | |
static QMetaObject::Connection | connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection) |
static QMetaObject::Connection | connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection) |
template<typename Func1 , typename Func2 > | |
static QMetaObject::Connection | connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection) |
static QMetaObject::Connection ::type | connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 slot) |
static QMetaObject::Connection ::type | connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *context, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection) |
template<typename Func1 , typename Func2 > | |
static std::enable_if< QtPrivate::FunctionPointer< Func2 >::ArgumentCount==-1 &&!std::is_convertible_v< Func2, const char * >, QMetaObject::Connection >::type | connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 slot) |
template<typename Func1 , typename Func2 > | |
static std::enable_if< QtPrivate::FunctionPointer< Func2 >::ArgumentCount==-1 &&!std::is_convertible_v< Func2, const char * >, QMetaObject::Connection >::type | connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *context, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection) |
static bool | disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member) |
static bool | disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member) |
static bool | disconnect (const QMetaObject::Connection &) |
template<typename Func1 , typename Func2 > | |
static bool | disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot) |
template<typename Func1 > | |
static bool | disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *receiver, void **zero) |
Protected Member Functions | |
void | timerEvent (QTimerEvent *) override |
Protected Member Functions inherited from QObject | |
QObject * | sender () const |
int | senderSignalIndex () const |
int | receivers (const char *signal) const |
bool | isSignalConnected (const QMetaMethod &signal) const |
virtual void | childEvent (QChildEvent *event) |
virtual void | customEvent (QEvent *event) |
virtual void | connectNotify (const QMetaMethod &signal) |
virtual void | disconnectNotify (const QMetaMethod &signal) |
QObject (QObjectPrivate &dd, QObject *parent=nullptr) | |
Properties | |
bool | singleShot |
whether the timer is a single-shot timer More... | |
int | interval |
the timeout interval in milliseconds More... | |
int | remainingTime |
the remaining time in milliseconds More... | |
Qt::TimerType | timerType |
controls the accuracy of the timer More... | |
bool | active |
Properties inherited from QObject | |
QString | objectName |
the name of this object More... | |
Additional Inherited Members | |
Protected Attributes inherited from QObject | |
QScopedPointer< QObjectData > | d_ptr |
Related Functions inherited from QObject | |
template< class T > T | qobject_cast (const QObject *object) |
template< typename T > T | qFindChildqFindChildren (const QObject *obj, const QString &name)() |
template< typename T > QList< T > | qFindChildrenqFindChildren (const QObject *obj, const QString &name)() |
QObjectList | |
The QTimer class provides repetitive and single-shot timers.
\inmodule QtCore
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.
Example for a one second (1000 millisecond) timer (from the \l{widgets/analogclock}{Analog Clock} example):
From then on, the update()
slot is called every second.
You can set a timer to time out only once by calling setSingleShot(true). You can also use the static QTimer::singleShot() function to call a slot after a specified interval:
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 \l{QObject::thread()}{thread affinity} to determine which thread will emit the \l{QTimer::}{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 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:
From then on, 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 \l{QThread}s.
Constructs a timer with the given parent.
Definition at line 167 of file qtimer.cpp.
QTimer::~QTimer | ( | ) |
Destroys the timer.
Definition at line 177 of file qtimer.cpp.
QBindable< bool > QTimer::bindableActive | ( | ) |
Definition at line 211 of file qtimer.cpp.
QBindable< int > QTimer::bindableInterval | ( | ) |
Definition at line 777 of file qtimer.cpp.
QBindable< bool > QTimer::bindableSingleShot | ( | ) |
Definition at line 740 of file qtimer.cpp.
QBindable< Qt::TimerType > QTimer::bindableTimerType | ( | ) |
Definition at line 821 of file qtimer.cpp.
|
inline |
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)}
.
Definition at line 167 of file qtimer.h.
int QTimer::interval | ( | ) | const |
Definition at line 772 of file qtimer.cpp.
bool QTimer::isActive | ( | ) | const |
Returns true
if the timer is running (pending); otherwise returns false.
Definition at line 206 of file qtimer.cpp.
bool QTimer::isSingleShot | ( | ) | const |
Definition at line 735 of file qtimer.cpp.
int QTimer::remainingTime | ( | ) | const |
Definition at line 793 of file qtimer.cpp.
void QTimer::setInterval | ( | int | msec | ) |
void QTimer::setSingleShot | ( | bool | singleShot | ) |
void QTimer::setTimerType | ( | Qt::TimerType | atype | ) |
Definition at line 811 of file qtimer.cpp.
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
\reentrant 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 \l{QObject::timerEvent()}{timerEvent} or create a local QTimer object.
Example:
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.
Definition at line 438 of file qtimer.cpp.
|
static |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. \reentrant 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 \l{QObject::timerEvent()}{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.
Definition at line 460 of file qtimer.cpp.
|
slot |
|
slot |
Starts or restarts the timer with a timeout interval of msec milliseconds.
If the timer is already running, it will be \l{QTimer::stop()}{stopped} and restarted.
If \l singleShot is true, the timer will be activated only once.
Definition at line 259 of file qtimer.cpp.
|
slot |
Stops the timer.
Definition at line 277 of file qtimer.cpp.
|
signal |
|
overrideprotectedvirtual |
int QTimer::timerId | ( | ) | const |
Returns the ID of the timer if the timer is running; otherwise returns -1.
Definition at line 222 of file qtimer.cpp.
Qt::TimerType QTimer::timerType | ( | ) | const |
Definition at line 816 of file qtimer.cpp.
|
read |
|
readwrite |
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().
|
read |
|
readwrite |
whether the timer is a single-shot timer
A single-shot timer fires only once, non-single-shot timers fire every \l interval milliseconds.
The default value for this property is false
.
|
readwrite |
controls the accuracy of the timer
The default value for this property is Qt::CoarseTimer
.