CopperSpice API  1.9.2
QTimer Class Reference

Provides repetitive and single-shot timers. More...

Inheritance diagram for QTimer:
QObject

Public Signals

void timeout ()
 
- Public Signals inherited from QObject
void destroyed (QObject *obj=nullptr)
 
void objectNameChanged (const QString &objectName)
 

Public Slots

void start ()
 
void start (int msec)
 
void stop ()
 
- Public Slots inherited from QObject
void deleteLater ()
 

Public Methods

 QTimer (QObject *parent=nullptr)
 
 ~QTimer ()
 
int interval () const
 
bool isActive () const
 
bool isSingleShot () const
 
int remainingTime () const
 
void setInterval (int msec)
 
void setSingleShot (bool value)
 
void setTimerType (Qt::TimerType timerType)
 
int timerId () const
 
Qt::TimerType timerType () const
 
- Public Methods inherited from QObject
 QObject (QObject *parent=nullptr)
 
 ~QObject ()
 
bool blockSignals (bool block)
 
const QList< QObject * > & children () const
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QStringdynamicPropertyNames () const
 
virtual bool event (QEvent *event)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
template<typename T >
findChild (const QString &childName=QString ()) const
 
template<class T >
QList< T > findChildren (const QRegularExpression &regExp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<class T >
QList< T > findChildren (const QString &childName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
bool inherits (const QString &className) const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const
 
bool isWindowType () const
 
void killTimer (int id)
 
const QMetaObjectmetaObject () const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const
 
QObject * parent () const
 
template<class T = QVariant>
property (const QString &name) const
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const QString &name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
QThreadthread () const
 

Static Public Methods

static void singleShot (int msec, const QObject *receiver, const QString &slotMethod)
 
template<typename T >
static void singleShot (int msec, QObject *receiver, T slotMethod)
 
static void singleShot (int msec, Qt::TimerType timerType, const QObject *receiver, const QString &slotMethod)
 
template<typename T >
static void singleShot (int msec, Qt::TimerType timerType, QObject *receiver, T slotMethod)
 
template<typename Receiver , typename SlotClass , typename SlotReturn >
static void singleShot (int msec, Qt::TimerType timerType, Receiver *receiver, SlotReturn (SlotClass::*slotMethod)())
 
template<typename T >
static void singleShot (int msec, Qt::TimerType timerType, T slotMethod)
 
template<typename Receiver , typename SlotClass , typename SlotReturn >
static void singleShot (int msec, Receiver *receiver, SlotReturn (SlotClass::*slotMethod)())
 
template<typename T >
static void singleShot (int msec, T slotMethod)
 
- Static Public Methods inherited from QObject
static bool connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ())
 
static bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, std::nullptr_t, const QObject *receiver, std::nullptr_t)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...))
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod)
 
static QMetaObjectstaticMetaObject ()
 
static QString tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >())
 

Protected Methods

void timerEvent (QTimerEvent *event) override
 
- Protected Methods inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signalMethod) const
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signalMethod) const
 
bool isSignalConnected (const QMetaMethod &signalMethod) const
 
int receivers (const QString &signal) const
 
QObject * sender () const
 
int senderSignalIndex () const
 

Properties

 active
 
 interval
 
 remainingTime
 
 singleShot
 
 timerType
 
- Properties inherited from QObject
 objectName
 

Additional Inherited Members

Detailed Description

The QTimer class provides repetitive and single-shot timers. This class provide a high level programming interface for timers.

First create a QTimer and then connect the timeout() signal to the appropriate slots. Call start() on the timer. From then on the timer will emit the timeout() signal at constant intervals.

QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);

In the example above, the update() slot is called once 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:

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(). CopperSpice uses the timer's thread affinity to determine which thread will emit the 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 CopperSpice 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 QThreads.

Accuracy and Timer Resolution

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.

The accuracy also depends on the timer type. For Qt::PreciseTimer, QTimer will try to keep the accuracy at 1 millisecond. Precise timers will also never time out earlier than expected.

For Qt::CoarseTimer and Qt::VeryCoarseTimer types, QTimer may wake up earlier than expected, within the margins for those types: 5% of the interval for Qt::CoarseTimer and 500 ms for Qt::VeryCoarseTimer.

All timer types may time out later than expected if the system is busy or unable to provide the requested accuracy. When this occurs the activated() signal will only be emitted once, even if multiple timeouts have expired, and then will resume the original interval.

Alternatives to QTimer

An alternative to using QTimer is to call QObject::startTimer() for your object and reimplement the QObject::timerEvent() event handler in your class (which must inherit QObject). The disadvantage is that 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. Ref to Timers for an overview of all three approaches.

Some operating systems limit the number of timers that may be used; CopperSpice tries to work around these limitations.

See also
QBasicTimer, QTimerEvent, QObject::timerEvent(), Timers

Constructor & Destructor Documentation

QTimer::QTimer ( QObject parent = nullptr)
explicit

Constructs a timer with the given parent.

QTimer::~QTimer ( )

Destroys the timer.

Method Documentation

int QTimer::interval ( ) const
inline

Returns the value of the property interval.

bool QTimer::isActive ( ) const
inline

Returns true if the timer is running or pending, otherwise returns false. This is a getter method for the active property.

bool QTimer::isSingleShot ( ) const
inline

Returns the value of the property singleShot.

See also
singleShot(), singleShot, interval
int QTimer::remainingTime ( ) const

Returns the value of the property remainingTime.

void QTimer::setInterval ( int  msec)

Sets the value of the property to msec.

void QTimer::setSingleShot ( bool  value)
inline

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.

See also
isSingleShot(), singleShot(), interval
void QTimer::setTimerType ( Qt::TimerType  timerType)
inline

Sets the value of the property to timerType.

void QTimer::singleShot ( int  msec,
const QObject receiver,
const QString slotMethod 
)
static

If msec is greater than 2000 the timerType will be Qt::CoarseTimer, otherwise it will be Qt::PreciseTimer.

Refer to singleShot() for more documentation.

template<typename T >
void QTimer::singleShot ( int  msec,
QObject receiver,
slotMethod 
)
static

If msec is greater than 2000 the timerType will be Qt::CoarseTimer, otherwise it will be Qt::PreciseTimer.

Refer to singleShot() for more documentation.

void QTimer::singleShot ( int  msec,
Qt::TimerType  timerType,
const QObject receiver,
const QString slotMethod 
)
static

A static method which calls a slot after a given time interval. This is a convenient way to invoke a slot without needing to add a timerEvent or create a local QTimer object. The time interval is msec milliseconds. The timeType is affects the accuracy of the timer. The receiver is the receiving object and the slotMethod is the slot.

If the receiver is destroyed before the interval msec occurs, this method will not be called. This method will be invoked in the thread of the receiver. The receiver's thread must have a running event loop.

The following sample program automatically terminates after 10 minutes (600,000 milliseconds).

#include <QApplication>
#include <QTimer>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QTimer::singleShot(600000, &app, SLOT(quit()));
// ...
return app.exec();
}
See also
setSingleShot(), start()
template<typename T >
void QTimer::singleShot ( int  msec,
Qt::TimerType  timerType,
QObject receiver,
slotMethod 
)
static

Refer to singleShot() for more documentation.

template<typename Receiver , typename SlotClass , typename SlotReturn >
void QTimer::singleShot ( int  msec,
Qt::TimerType  timerType,
Receiver *  receiver,
SlotReturn (SlotClass::*)()  slotMethod 
)
static

Refer to singleShot() for more documentation.

template<typename T >
void QTimer::singleShot ( int  msec,
Qt::TimerType  timerType,
slotMethod 
)
static

Refer to singleShot() for more documentation.

template<typename Receiver , typename SlotClass , typename SlotReturn >
void QTimer::singleShot ( int  msec,
Receiver *  receiver,
SlotReturn (SlotClass::*)()  slotMethod 
)
static

If msec is greater than 2000 the timerType will be Qt::CoarseTimer, otherwise it will be Qt::PreciseTimer.

Refer to singleShot() for more documentation.

See also
isSingleShot(), singleShot, interval
template<typename T >
void QTimer::singleShot ( int  msec,
slotMethod 
)
static

If msec is greater than 2000 the timerType will be Qt::CoarseTimer, otherwise it will be Qt::PreciseTimer.

Refer to singleShot() for more documentation.

void QTimer::start ( )
slot

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.

void QTimer::start ( int  msec)
slot

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.

void QTimer::stop ( )
slot

Stops the timer.

See also
start()
void QTimer::timeout ( )
signal

This signal is emitted when the timer times out.

See also
interval, start(), stop()
void QTimer::timerEvent ( QTimerEvent event)
overrideprotectedvirtual

Reimplemented from QObject::timerEvent()

int QTimer::timerId ( ) const
inline

Returns the ID of the timer if the timer is running, otherwise returns -1.

Qt::TimerType QTimer::timerType ( ) const
inline

Returns the value of the property timerType.

Property Documentation

QTimer::active

This boolean property is true if the timer is running, otherwise false.

PropertiesClass Methods
read isActive
QTimer::interval

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().

See also
singleShot
PropertiesClass Methods
read interval
write setInterval
QTimer::remainingTime

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.

See also
interval
PropertiesClass Methods
read remainingTime
QTimer::singleShot

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.

See also
interval
PropertiesClass Methods
read isSingleShot
write setSingleShot
QTimer::timerType

This property contains an enum value which indicate the accuracy level of the timer. The default value for this property is Qt::CoarseTimer.

See also
Qt::TimerType
PropertiesClass Methods
read timerType
write setTimerType