添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
帅气的蚂蚁  ·  Using Spline with ...·  3 天前    · 
苦闷的围巾  ·  const (C++) | ...·  3 天前    · 
着急的跑步鞋  ·  Fix 'Binding Element ...·  2 天前    · 
朝气蓬勃的黑框眼镜  ·  Hooks | Taro 文档·  2 天前    · 
还单身的红金鱼  ·  GitSwarm 2016.3-2 ...·  7 月前    · 
发呆的骆驼  ·  Products - Unified ...·  1 年前    · 
跑龙套的玉米  ·  Azure AD B2C ...·  1 年前    · 
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 &regExp)() 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...

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

Definition at line 147 of file qtimer.cpp .

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

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

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

69 { return id >= 0; }

isSingleShot()

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

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

108 { single = asingleShot; }
uint single
Definition: qtimer.h:104

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

250 {
251 inter = msec;
252 start ();
253 }
int inter
Definition: qtimer.h:103
void start()
Definition: qtimer.cpp:231

Referenced by start() .

232 {
233 if ( id != INV_TIMER ) // stop running timer
234 stop ();
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

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

285 {
286 if ( id != INV_TIMER ) {
288 id = INV_TIMER ;
289 }
290 }
static const int INV_TIMER
Definition: qtimer.cpp:141
void killTimer(int id)
Kills the timer with timer identifier, id.
Definition: qobject.cpp:1650

Definition at line 296 of file qtimer.cpp .

297 {
298 if (e-> timerId () == id ) {
299 if ( single )
300 stop ();
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

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

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 .

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: