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

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

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