| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information ([email protected])
|
|---|
| 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 [email protected].
|
|---|
| 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:
|
|---|
|
|---|