| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the documentation of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | /*!
|
|---|
| 43 | \page timers.html
|
|---|
| 44 | \title Timers
|
|---|
| 45 | \ingroup architecture
|
|---|
| 46 | \brief How to use timers in your application.
|
|---|
| 47 |
|
|---|
| 48 | QObject, the base class of all Qt objects, provides the basic
|
|---|
| 49 | timer support in Qt. With QObject::startTimer(), you start a
|
|---|
| 50 | timer with an interval in milliseconds as argument. The function
|
|---|
| 51 | returns a unique integer timer ID. The timer will now fire at
|
|---|
| 52 | regular intervals until you explicitly call QObject::killTimer()
|
|---|
| 53 | with the timer ID.
|
|---|
| 54 |
|
|---|
| 55 | For this mechanism to work, the application must run in an event
|
|---|
| 56 | loop. You start an event loop with QApplication::exec(). When a
|
|---|
| 57 | timer fires, the application sends a QTimerEvent, and the flow of
|
|---|
| 58 | control leaves the event loop until the timer event is processed.
|
|---|
| 59 | This implies that a timer cannot fire while your application is
|
|---|
| 60 | busy doing something else. In other words: the accuracy of timers
|
|---|
| 61 | depends on the granularity of your application.
|
|---|
| 62 |
|
|---|
| 63 | In multithreaded applications, you can use the timer mechanism in
|
|---|
| 64 | any thread that has an event loop. To start an event loop from a
|
|---|
| 65 | non-GUI thread, use QThread::exec(). Qt uses the the object's
|
|---|
| 66 | \l{QObject::thread()}{thread affinity} to determine which thread
|
|---|
| 67 | will deliver the QTimerEvent. Because of this, you must start and
|
|---|
| 68 | stop all timers in the object's thread; it is not possible to
|
|---|
| 69 | start timers for objects in another thread.
|
|---|
| 70 |
|
|---|
| 71 | The upper limit for the interval value is determined by the number
|
|---|
| 72 | of milliseconds that can be specified in a signed integer
|
|---|
| 73 | (in practice, this is a period of just over 24 days). The accuracy
|
|---|
| 74 | depends on the underlying operating system. Windows 98 has 55
|
|---|
| 75 | millisecond accuracy; other systems that we have tested can handle
|
|---|
| 76 | 1 millisecond intervals.
|
|---|
| 77 |
|
|---|
| 78 | The main API for the timer functionality is QTimer. That class
|
|---|
| 79 | provides regular timers that emit a signal when the timer fires, and
|
|---|
| 80 | inherits QObject so that it fits well into the ownership structure
|
|---|
| 81 | of most GUI programs. The normal way of using it is like this:
|
|---|
| 82 |
|
|---|
| 83 | \snippet doc/src/snippets/timers/timers.cpp 0
|
|---|
| 84 | \snippet doc/src/snippets/timers/timers.cpp 1
|
|---|
| 85 | \snippet doc/src/snippets/timers/timers.cpp 2
|
|---|
| 86 |
|
|---|
| 87 | The QTimer object is made into a child of this widget so that,
|
|---|
| 88 | when this widget is deleted, the timer is deleted too.
|
|---|
| 89 | Next, its \l{QTimer::}{timeout()} signal is connected to the slot
|
|---|
| 90 | that will do the work, it is started with a value of 1000
|
|---|
| 91 | milliseconds, indicating that it will time out every second.
|
|---|
| 92 |
|
|---|
| 93 | QTimer also provides a static function for single-shot timers.
|
|---|
| 94 | For example:
|
|---|
| 95 |
|
|---|
| 96 | \snippet doc/src/snippets/timers/timers.cpp 3
|
|---|
| 97 |
|
|---|
| 98 | 200 milliseconds (0.2 seconds) after this line of code is
|
|---|
| 99 | executed, the \c updateCaption() slot will be called.
|
|---|
| 100 |
|
|---|
| 101 | For QTimer to work, you must have an event loop in your
|
|---|
| 102 | application; that is, you must call QCoreApplication::exec()
|
|---|
| 103 | somewhere. Timer events will be delivered only while the event
|
|---|
| 104 | loop is running.
|
|---|
| 105 |
|
|---|
| 106 | In multithreaded applications, you can use QTimer in any thread
|
|---|
| 107 | that has an event loop. To start an event loop from a non-GUI
|
|---|
| 108 | thread, use QThread::exec(). Qt uses the the timer's
|
|---|
| 109 | \l{QObject::thread()}{thread affinity} to determine which thread
|
|---|
| 110 | will emit the \l{QTimer::}{timeout()} signal. Because of this, you
|
|---|
| 111 | must start and stop the timer in its thread; it is not possible to
|
|---|
| 112 | start a timer from another thread.
|
|---|
| 113 |
|
|---|
| 114 | The \l{widgets/analogclock}{Analog Clock} example shows how to use
|
|---|
| 115 | QTimer to redraw a widget at regular intervals. From \c{AnalogClock}'s
|
|---|
| 116 | implementation:
|
|---|
| 117 |
|
|---|
| 118 | \snippet examples/widgets/analogclock/analogclock.cpp 0
|
|---|
| 119 | \snippet examples/widgets/analogclock/analogclock.cpp 2
|
|---|
| 120 | \snippet examples/widgets/analogclock/analogclock.cpp 3
|
|---|
| 121 | \snippet examples/widgets/analogclock/analogclock.cpp 4
|
|---|
| 122 | \snippet examples/widgets/analogclock/analogclock.cpp 5
|
|---|
| 123 | \snippet examples/widgets/analogclock/analogclock.cpp 6
|
|---|
| 124 | \dots
|
|---|
| 125 | \snippet examples/widgets/analogclock/analogclock.cpp 7
|
|---|
| 126 |
|
|---|
| 127 | Every second, QTimer will call the QWidget::update() slot to
|
|---|
| 128 | refresh the clock's display.
|
|---|
| 129 |
|
|---|
| 130 | If you already have a QObject subclass and want an easy
|
|---|
| 131 | optimization, you can use QBasicTimer instead of QTimer. With
|
|---|
| 132 | QBasicTimer, you must reimplement
|
|---|
| 133 | \l{QObject::timerEvent()}{timerEvent()} in your QObject subclass
|
|---|
| 134 | and handle the timeout there. The \l{widgets/wiggly}{Wiggly}
|
|---|
| 135 | example shows how to use QBasicTimer.
|
|---|
| 136 | */
|
|---|