Qt 4.8
Public Slots | Signals | Public Functions | Static Public Functions | Protected Functions | Private Functions | Properties | List of all members
QTimer Class Reference

The QTimer class provides repetitive and single-shot timers. More...

#include <qtimer.h>

Inheritance diagram for QTimer:
QObject

Public Slots

void start (int msec)
 Starts or restarts the timer with a timeout interval of msec milliseconds. More...
 
void start ()
 
void stop ()
 Stops the timer. More...
 
- Public Slots inherited from QObject
void deleteLater ()
 Schedules this object for deletion. More...
 

Signals

void timeout ()
 This signal is emitted when the timer times out. More...
 
- Signals inherited from QObject
void destroyed (QObject *=0)
 This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More...
 

Public Functions

int interval () const
 
bool isActive () const
 Returns true if the timer is running (pending); otherwise returns false. More...
 
bool isSingleShot () const
 
 QTimer (QObject *parent=0)
 Constructs a timer with the given parent. More...
 
void setInterval (int msec)
 
void setSingleShot (bool singleShot)
 
int timerId () const
 Returns the ID of the timer if the timer is running; otherwise returns -1. More...
 
 ~QTimer ()
 Destroys the timer. More...
 
- Public Functions inherited from QObject
bool blockSignals (bool b)
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More...
 
const QObjectListchildren () const
 Returns a list of child objects. More...
 
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
 
bool disconnect (const QObject *receiver, const char *member=0)
 
void dumpObjectInfo ()
 Dumps information about signal connections, etc. More...
 
void dumpObjectTree ()
 Dumps a tree of children to the debug output. More...
 
QList< QByteArraydynamicPropertyNames () const
 Returns the names of all properties that were dynamically added to the object using setProperty(). More...
 
virtual bool event (QEvent *)
 This virtual function receives events to an object and should return true if the event e was recognized and processed. More...
 
virtual bool eventFilter (QObject *, QEvent *)
 Filters events if this object has been installed as an event filter for the watched object. More...
 
template<typename T >
findChild (const QString &aName=QString()) const
 Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More...
 
template<typename T >
QList< T > findChildren (const QString &aName=QString()) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More...
 
template<typename T >
QList< T > findChildren (const QRegExp &re) const
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More...
 
void installEventFilter (QObject *)
 Installs an event filter filterObj on this object. More...
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false. More...
 
void killTimer (int id)
 Kills the timer with timer identifier, id. More...
 
virtual const QMetaObjectmetaObject () const
 Returns a pointer to the meta-object of this object. More...
 
void moveToThread (QThread *thread)
 Changes the thread affinity for this object and its children. More...
 
QString objectName () const
 
QObjectparent () const
 Returns a pointer to the parent object. More...
 
QVariant property (const char *name) const
 Returns the value of the object's name property. More...
 
Q_INVOKABLE QObject (QObject *parent=0)
 Constructs an object with parent object parent. More...
 
void removeEventFilter (QObject *)
 Removes an event filter object obj from this object. More...
 
void setObjectName (const QString &name)
 
void setParent (QObject *)
 Makes the object a child of parent. More...
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value. More...
 
void setUserData (uint id, QObjectUserData *data)
 
bool signalsBlocked () const
 Returns true if signals are blocked; otherwise returns false. More...
 
int startTimer (int interval)
 Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (uint id) const
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects. More...
 

Static Public Functions

static void singleShot (int msec, QObject *receiver, const char *member)
 
- Static Public Functions inherited from QObject
static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More...
 
static bool connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 Disconnects signal in object sender from method in object receiver. More...
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static uint registerUserData ()
 
static QString tr (const char *sourceText, const char *comment=0, int n=-1)
 
static QString trUtf8 (const char *sourceText, const char *comment=0, int n=-1)
 

Protected Functions

void timerEvent (QTimerEvent *)
 Reimplemented Function More...
 
- Protected Functions inherited from QObject
virtual void childEvent (QChildEvent *)
 This event handler can be reimplemented in a subclass to receive child events. More...
 
virtual void connectNotify (const char *signal)
 This virtual function is called when something has been connected to signal in this object. More...
 
virtual void customEvent (QEvent *)
 This event handler can be reimplemented in a subclass to receive custom events. More...
 
virtual void disconnectNotify (const char *signal)
 This virtual function is called when something has been disconnected from signal in this object. More...
 
 QObject (QObjectPrivate &dd, QObject *parent=0)
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal. More...
 
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More...
 
int senderSignalIndex () const
 

Private Functions

void killTimer (int)
 
int startTimer (int)
 

Properties

bool active
 This boolean property is true if the timer is running; otherwise false. More...
 
int del
 
int id
 
int inter
 
int interval
 the timeout interval in milliseconds More...
 
uint nulltimer: 1
 
uint single: 1
 
bool singleShot
 This static function calls a slot after a given time interval. More...
 

Additional Inherited Members

- Static Public Variables inherited from QObject
static const QMetaObject staticMetaObject
 This variable stores the meta-object for the class. More...
 
- Protected Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

The QTimer class provides repetitive and single-shot timers.

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 Analog Clock example):

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

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:

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 thread affinity to determine which thread will emit the 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 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.

Accuracy and Timer Resolution

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.

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. See Timers 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.

See also
QBasicTimer, QTimerEvent, QObject::timerEvent(), Timers, {Analog Clock Example}, {Wiggly Example}

Definition at line 56 of file qtimer.h.

Constructors and Destructors

◆ QTimer()

QTimer::QTimer ( QObject parent = 0)
explicit

Constructs a timer with the given parent.

Definition at line 147 of file qtimer.cpp.

148  : QObject(parent), id(INV_TIMER), inter(0), del(0), single(0), nulltimer(0)
149 {
150 }
int del
Definition: qtimer.h:103
uint nulltimer
Definition: qtimer.h:105
static const int INV_TIMER
Definition: qtimer.cpp:141
int id
Definition: qtimer.h:103
Q_INVOKABLE QObject(QObject *parent=0)
Constructs an object with parent object parent.
Definition: qobject.cpp:753
int inter
Definition: qtimer.h:103
uint single
Definition: qtimer.h:104

◆ ~QTimer()

QTimer::~QTimer ( )

Destroys the timer.

Definition at line 169 of file qtimer.cpp.

170 {
171  if (id != INV_TIMER) // stop running timer
172  stop();
173 }
static const int INV_TIMER
Definition: qtimer.cpp:141
void stop()
Stops the timer.
Definition: qtimer.cpp:284

Functions

◆ interval()

int QTimer::interval ( ) const
inline

Definition at line 73 of file qtimer.h.

73 { return inter; }
int inter
Definition: qtimer.h:103

◆ isActive()

bool QTimer::isActive ( ) const
inline

◆ isSingleShot()

bool QTimer::isSingleShot ( ) const
inline

Definition at line 76 of file qtimer.h.

76 { return single; }
uint single
Definition: qtimer.h:104

◆ killTimer()

void QTimer::killTimer ( int  )
inlineprivate

Definition at line 101 of file qtimer.h.

101 {}

◆ setInterval()

void QTimer::setInterval ( int  msec)

Definition at line 419 of file qtimer.cpp.

Referenced by QFbScreen::QFbScreen(), QSmoothedAnimation::QSmoothedAnimation(), start(), and QNetworkConfigurationManagerPrivate::startPolling().

420 {
421  inter = msec;
422  if (id != INV_TIMER) { // create new timer
423  QObject::killTimer(id); // restart timer
424  id = QObject::startTimer(msec);
425  }
426 }
int startTimer(int interval)
Starts a timer and returns a timer identifier, or returns zero if it could not start a timer...
Definition: qobject.cpp:1623
static const int INV_TIMER
Definition: qtimer.cpp:141
int inter
Definition: qtimer.h:103
void killTimer(int id)
Kills the timer with timer identifier, id.
Definition: qobject.cpp:1650

◆ setSingleShot()

void QTimer::setSingleShot ( bool  singleShot)
inline

◆ singleShot()

static void QTimer::singleShot ( int  msec,
QObject receiver,
const char *  member 
)
static

◆ start [1/2]

void QTimer::start ( int  msec)
slot

◆ start [2/2]

void QTimer::start ( )
slot

Definition at line 231 of file qtimer.cpp.

Referenced by start().

232 {
233  if (id != INV_TIMER) // stop running timer
234  stop();
235  nulltimer = (!inter && single);
237 }
int startTimer(int interval)
Starts a timer and returns a timer identifier, or returns zero if it could not start a timer...
Definition: qobject.cpp:1623
uint nulltimer
Definition: qtimer.h:105
static const int INV_TIMER
Definition: qtimer.cpp:141
int inter
Definition: qtimer.h:103
uint single
Definition: qtimer.h:104
void stop()
Stops the timer.
Definition: qtimer.cpp:284

◆ startTimer()

int QTimer::startTimer ( int  )
inlineprivate

Definition at line 100 of file qtimer.h.

100 { return -1;}

◆ stop

void QTimer::stop ( )
slot

◆ timeout

void QTimer::timeout ( )
signal

This signal is emitted when the timer times out.

See also
interval, start(), stop()

Referenced by timerEvent().

◆ timerEvent()

void QTimer::timerEvent ( QTimerEvent e)
protectedvirtual

Reimplemented Function

Reimplemented from QObject.

Definition at line 296 of file qtimer.cpp.

297 {
298  if (e->timerId() == id) {
299  if (single)
300  stop();
301  emit timeout();
302  }
303 }
#define emit
Definition: qobjectdefs.h:76
int timerId() const
Returns the unique timer identifier, which is the same identifier as returned from QObject::startTime...
Definition: qcoreevent.h:346
void timeout()
This signal is emitted when the timer times out.
int id
Definition: qtimer.h:103
uint single
Definition: qtimer.h:104
void stop()
Stops the timer.
Definition: qtimer.cpp:284

◆ timerId()

int QTimer::timerId ( ) const
inline

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

Definition at line 70 of file qtimer.h.

Referenced by start().

70 { return id; }
int id
Definition: qtimer.h:103

Properties

◆ active

QTimer::active
private

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

Since
4.3

Definition at line 61 of file qtimer.h.

◆ del

int QTimer::del
private

Definition at line 103 of file qtimer.h.

◆ id

int QTimer::id
private

Definition at line 103 of file qtimer.h.

Referenced by QTimer(), start(), and timerEvent().

◆ inter

int QTimer::inter
private

Definition at line 103 of file qtimer.h.

Referenced by start().

◆ interval

QTimer::interval
private

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

Definition at line 60 of file qtimer.h.

◆ nulltimer

uint QTimer::nulltimer
private

Definition at line 105 of file qtimer.h.

Referenced by QTimer(), and start().

◆ single

uint QTimer::single
private

Definition at line 104 of file qtimer.h.

Referenced by QTimer(), setSingleShot(), start(), and timerEvent().

◆ singleShot

QTimer::singleShot
private

This static function calls a slot after a given time interval.

whether the timer is a single-shot timer

Note
This class or function is reentrant.

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.

See also
start()

A single-shot timer fires only once, non-single-shot timers fire every interval milliseconds.

See also
interval, singleShot()

Definition at line 59 of file qtimer.h.

Referenced by QWSServerPrivate::_q_clientClosed(), QLocalSocketPrivate::_q_notified(), QFtpPrivate::addCommand(), QLocalSocketPrivate::checkPipeState(), QLocalSocketPrivate::checkReadyRead(), QDeclarativeObjectScriptClass::destroy(), QLocalSocketPrivate::destroyPipeHandles(), QWSServerPrivate::disconnectClient(), QLineEdit::event(), QXlibScreen::eventDispatcher(), QWSSoundServerPrivate::feedDevice(), QAudioOutputPrivate::freeBlocks(), QAudioInputPrivate::freeBlocks(), QMeeGoGraphicsSystemSwitchHandler::handleMapNotify(), QMenuPrivate::hideMenu(), QComboBox::hidePopup(), QMenuPrivate::hideUpToMenuBar(), QDeferredGraphicsSystemChange::launch(), QColorDialogPrivate::mac_nativeDialogModalHelp(), QFontDialogPrivate::mac_nativeDialogModalHelp(), QFileDialogPrivate::mac_nativeDialogModalHelp(), QNetworkSessionPrivateImpl::open(), QMacWindowFader::performFade(), QDeclarativeXmlListModel::reload(), QMeeGoGraphicsSystemSwitchHandler::removeWidget(), QDeclarativeXmlListModel::requestFinished(), QConnmanEngine::requestUpdate(), QDeclarativeBasePositionerPrivate::schedulePositioning(), QSyntaxHighlighter::setDocument(), QHttpThreadDelegate::startRequestSynchronously(), QCoreFuriCuri::timerEvent(), QSingleShotTimer::timerEvent(), QBBEngine::updateConfiguration(), and QNetworkSession::waitForOpened().


The documentation for this class was generated from the following files: