source: trunk/doc/src/howtos/timers.qdoc@ 564

Last change on this file since 564 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

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