Qt 4.8
|
The QTimer class provides repetitive and single-shot timers. More...
#include <qtimer.h>
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 QObjectList & | children () 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< QByteArray > | dynamicPropertyNames () 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 > | |
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 QMetaObject * | metaObject () 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 |
QObject * | parent () 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... | |
QThread * | thread () const |
Returns the thread in which the object lives. More... | |
QObjectUserData * | userData (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... | |
QObject * | sender () 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< QObjectData > | d_ptr |
Static Protected Variables inherited from QObject | |
static const QMetaObject | staticQtMetaObject |
Related Functions inherited from QObject | |
T | qFindChildqFindChildren (const QObject *obj, const QString &name)() |
QList< T > | qFindChildrenqFindChildren (const QObject *obj, const QString &name)() |
QList< T > | qFindChildrenqFindChildren (const QObject *obj, const QRegExp ®Exp)() |
T * | qobject_cast (QObject *object) |
QObjectList | |
void * | qt_find_obj_child (QObject *parent, const char *type, const QString &name) |
Returns a pointer to the object named name that inherits type and with a given parent. More... | |
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):
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 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:
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.
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.
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.
|
explicit |
Constructs a timer with the given parent.
Definition at line 147 of file qtimer.cpp.
QTimer::~QTimer | ( | ) |
|
inline |
Returns true if the timer is running (pending); otherwise returns false.
Definition at line 69 of file qtimer.h.
Referenced by Maemo::IcdPrivate::addrinfo(), QIcdEngine::cancelAsyncConfigurationUpdate(), QNetworkSessionPrivateImpl::close(), QSmoothedAnimation::delayedStop(), QComboBoxPrivateContainer::eventFilter(), QSmoothedAnimation::init(), QComboBoxPrivateContainer::mouseReleaseEvent(), QNetworkSessionPrivateImpl::open(), Maemo::IcdPrivate::scan(), QWSServer::screenSaverActive(), QFbScreen::setDirty(), QVNCServer::setDirty(), QNetworkConfigurationManagerPrivate::startPolling(), Maemo::IcdPrivate::state(), QNetworkSessionPrivateImpl::stateChange(), Maemo::IcdPrivate::statistics(), QNetworkSessionPrivateImpl::stop(), and QNetworkSessionPrivateImpl::updateState().
void QTimer::setInterval | ( | int | msec | ) |
Definition at line 419 of file qtimer.cpp.
Referenced by QFbScreen::QFbScreen(), QSmoothedAnimation::QSmoothedAnimation(), start(), and QNetworkConfigurationManagerPrivate::startPolling().
|
inline |
Definition at line 108 of file qtimer.h.
Referenced by Maemo::IcdPrivate::addrinfo(), QApplication::alert(), QIcdEngine::ensureDBusConnection(), QHttpPrivate::init(), QVNCServer::init(), QComboBoxPrivateContainer::QComboBoxPrivateContainer(), QFbScreen::QFbScreen(), QPatternist::QIODeviceDelegate::QIODeviceDelegate(), QMoviePrivate::QMoviePrivate(), QSmoothedAnimation::QSmoothedAnimation(), QWSKbPrivate::QWSKbPrivate(), Maemo::IcdPrivate::scan(), start(), QNetworkConfigurationManagerPrivate::startPolling(), Maemo::IcdPrivate::state(), Maemo::IcdPrivate::statistics(), and QApplication::syncX().
|
static |
|
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.
Definition at line 249 of file qtimer.cpp.
Referenced by QAbstractSocketPrivate::_q_connectToNextAddress(), QMoviePrivate::_q_loadNextFrame(), QHttpPrivate::_q_slotConnected(), Maemo::IcdPrivate::addrinfo(), QApplication::alert(), QWSKbPrivate::autoRepeat(), QWSKbPrivate::beginAutoRepeat(), QSmoothedAnimation::delayedStop(), QNetworkSessionPrivateImpl::do_open(), loadDocument(), QVNCServer::newConnection(), QAudioInputPrivate::open(), QAudioOutputPrivate::open(), QProcessPrivate::pipeWriterBytesToWrite(), QPatternist::QIODeviceDelegate::QIODeviceDelegate(), qt_create_commandline(), QIcdEngine::requestUpdate(), QAudioInputPrivate::resume(), QAudioOutputPrivate::resume(), QAlphaWidget::run(), QPollingFileSystemWatcherEngine::run(), QRollEffect::run(), Maemo::IcdPrivate::scan(), QFbScreen::setDirty(), QVNCServer::setDirty(), QtMultimediaInternal::QAudioOutputBuffer::startFillTimer(), QNetworkConfigurationManagerPrivate::startPolling(), Maemo::IcdPrivate::state(), Maemo::IcdPrivate::statistics(), and QApplication::syncX().
|
slot |
Definition at line 231 of file qtimer.cpp.
Referenced by start().
|
inlineprivate |
|
slot |
Stops the timer.
Definition at line 284 of file qtimer.cpp.
Referenced by QAbstractSocketPrivate::_q_abortConnectionAttempt(), QHttpPrivate::_q_slotReadyRead(), QAbstractSocketPrivate::_q_testConnection(), Maemo::IcdPrivate::addrinfo(), QIcdEngine::cancelAsyncConfigurationUpdate(), QIcdEngine::cleanup(), QAudioInputPrivate::close(), QAudioOutputPrivate::close(), QNetworkSessionPrivateImpl::close(), QWSKbPrivate::endAutoRepeat(), QComboBoxPrivateContainer::eventFilter(), QtMultimediaInternal::QAudioOutputBuffer::fillBuffer(), QSmoothedAnimation::init(), QMoviePrivate::jumpToFrame(), loadDocument(), QNetworkSessionPrivateImpl::open(), QProcessPrivate::pipeWriterBytesToWrite(), QAlphaWidget::render(), QMoviePrivate::reset(), QAbstractSocketPrivate::resetSocketLayer(), Maemo::IcdPrivate::scan(), QRollEffect::scroll(), QWSServer::setScreenSaverIntervals(), start(), Maemo::IcdPrivate::state(), QNetworkSessionPrivateImpl::stateChange(), Maemo::IcdPrivate::statistics(), QNetworkSessionPrivateImpl::stop(), QtMultimediaInternal::QAudioOutputBuffer::stopFillTimer(), QAudioInputPrivate::suspend(), QAudioOutputPrivate::suspend(), timerEvent(), QNetworkSessionPrivateImpl::updateState(), and ~QTimer().
|
signal |
This signal is emitted when the timer times out.
Referenced by timerEvent().
|
protectedvirtual |
Reimplemented Function
Reimplemented from QObject.
Definition at line 296 of file qtimer.cpp.
|
inline |
|
private |
|
private |
Definition at line 103 of file qtimer.h.
Referenced by QTimer(), start(), and timerEvent().
|
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().
|
private |
|
private |
Definition at line 104 of file qtimer.h.
Referenced by QTimer(), setSingleShot(), start(), and timerEvent().
|
private |
This static function calls a slot after a given time interval.
whether the timer is a single-shot timer
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:
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.
A single-shot timer fires only once, non-single-shot timers fire every interval milliseconds.
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().