Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/kernel/qtimer.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||
2 | ** | - | ||||||||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||
5 | ** | - | ||||||||||||||||||
6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||||||||
7 | ** | - | ||||||||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||
14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||||||||
16 | ** | - | ||||||||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||
19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||
25 | ** | - | ||||||||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||
29 | ** | - | ||||||||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
31 | ** | - | ||||||||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||||||||
33 | - | |||||||||||||||||||
34 | #include "qtimer.h" | - | ||||||||||||||||||
35 | #include "qabstracteventdispatcher.h" | - | ||||||||||||||||||
36 | #include "qcoreapplication.h" | - | ||||||||||||||||||
37 | #include "qobject_p.h" | - | ||||||||||||||||||
38 | - | |||||||||||||||||||
39 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
40 | - | |||||||||||||||||||
41 | /*! | - | ||||||||||||||||||
42 | \class QTimer | - | ||||||||||||||||||
43 | \inmodule QtCore | - | ||||||||||||||||||
44 | \brief The QTimer class provides repetitive and single-shot timers. | - | ||||||||||||||||||
45 | - | |||||||||||||||||||
46 | \ingroup events | - | ||||||||||||||||||
47 | - | |||||||||||||||||||
48 | - | |||||||||||||||||||
49 | The QTimer class provides a high-level programming interface for | - | ||||||||||||||||||
50 | timers. To use it, create a QTimer, connect its timeout() signal | - | ||||||||||||||||||
51 | to the appropriate slots, and call start(). From then on, it will | - | ||||||||||||||||||
52 | emit the timeout() signal at constant intervals. | - | ||||||||||||||||||
53 | - | |||||||||||||||||||
54 | Example for a one second (1000 millisecond) timer (from the | - | ||||||||||||||||||
55 | \l{widgets/analogclock}{Analog Clock} example): | - | ||||||||||||||||||
56 | - | |||||||||||||||||||
57 | \snippet ../widgets/widgets/analogclock/analogclock.cpp 4 | - | ||||||||||||||||||
58 | \snippet ../widgets/widgets/analogclock/analogclock.cpp 5 | - | ||||||||||||||||||
59 | \snippet ../widgets/widgets/analogclock/analogclock.cpp 6 | - | ||||||||||||||||||
60 | - | |||||||||||||||||||
61 | From then on, the \c update() slot is called every second. | - | ||||||||||||||||||
62 | - | |||||||||||||||||||
63 | You can set a timer to time out only once by calling | - | ||||||||||||||||||
64 | setSingleShot(true). You can also use the static | - | ||||||||||||||||||
65 | QTimer::singleShot() function to call a slot after a specified | - | ||||||||||||||||||
66 | interval: | - | ||||||||||||||||||
67 | - | |||||||||||||||||||
68 | \snippet timers/timers.cpp 3 | - | ||||||||||||||||||
69 | - | |||||||||||||||||||
70 | In multithreaded applications, you can use QTimer in any thread | - | ||||||||||||||||||
71 | that has an event loop. To start an event loop from a non-GUI | - | ||||||||||||||||||
72 | thread, use QThread::exec(). Qt uses the timer's | - | ||||||||||||||||||
73 | \l{QObject::thread()}{thread affinity} to determine which thread | - | ||||||||||||||||||
74 | will emit the \l{QTimer::}{timeout()} signal. Because of this, you | - | ||||||||||||||||||
75 | must start and stop the timer in its thread; it is not possible to | - | ||||||||||||||||||
76 | start a timer from another thread. | - | ||||||||||||||||||
77 | - | |||||||||||||||||||
78 | As a special case, a QTimer with a timeout of 0 will time out as | - | ||||||||||||||||||
79 | soon as all the events in the window system's event queue have | - | ||||||||||||||||||
80 | been processed. This can be used to do heavy work while providing | - | ||||||||||||||||||
81 | a snappy user interface: | - | ||||||||||||||||||
82 | - | |||||||||||||||||||
83 | \snippet timers/timers.cpp 4 | - | ||||||||||||||||||
84 | \snippet timers/timers.cpp 5 | - | ||||||||||||||||||
85 | \snippet timers/timers.cpp 6 | - | ||||||||||||||||||
86 | - | |||||||||||||||||||
87 | From then on, \c processOneThing() will be called repeatedly. It | - | ||||||||||||||||||
88 | should be written in such a way that it always returns quickly | - | ||||||||||||||||||
89 | (typically after processing one data item) so that Qt can deliver | - | ||||||||||||||||||
90 | events to the user interface and stop the timer as soon as it has done all | - | ||||||||||||||||||
91 | its work. This is the traditional way of implementing heavy work | - | ||||||||||||||||||
92 | in GUI applications, but as multithreading is nowadays becoming available on | - | ||||||||||||||||||
93 | more and more platforms, we expect that zero-millisecond | - | ||||||||||||||||||
94 | QTimer objects will gradually be replaced by \l{QThread}s. | - | ||||||||||||||||||
95 | - | |||||||||||||||||||
96 | \section1 Accuracy and Timer Resolution | - | ||||||||||||||||||
97 | - | |||||||||||||||||||
98 | The accuracy of timers depends on the underlying operating system | - | ||||||||||||||||||
99 | and hardware. Most platforms support a resolution of 1 millisecond, | - | ||||||||||||||||||
100 | though the accuracy of the timer will not equal this resolution | - | ||||||||||||||||||
101 | in many real-world situations. | - | ||||||||||||||||||
102 | - | |||||||||||||||||||
103 | The accuracy also depends on the \l{Qt::TimerType}{timer type}. For | - | ||||||||||||||||||
104 | Qt::PreciseTimer, QTimer will try to keep the accurance at 1 millisecond. | - | ||||||||||||||||||
105 | Precise timers will also never time out earlier than expected. | - | ||||||||||||||||||
106 | - | |||||||||||||||||||
107 | For Qt::CoarseTimer and Qt::VeryCoarseTimer types, QTimer may wake up | - | ||||||||||||||||||
108 | earlier than expected, within the margins for those types: 5% of the | - | ||||||||||||||||||
109 | interval for Qt::CoarseTimer and 500 ms for Qt::VeryCoarseTimer. | - | ||||||||||||||||||
110 | - | |||||||||||||||||||
111 | All timer types may time out later than expected if the system is busy or | - | ||||||||||||||||||
112 | unable to provide the requested accuracy. In such a case of timeout | - | ||||||||||||||||||
113 | overrun, Qt will emit activated() only once, even if multiple timeouts have | - | ||||||||||||||||||
114 | expired, and then will resume the original interval. | - | ||||||||||||||||||
115 | - | |||||||||||||||||||
116 | \section1 Alternatives to QTimer | - | ||||||||||||||||||
117 | - | |||||||||||||||||||
118 | An alternative to using QTimer is to call QObject::startTimer() | - | ||||||||||||||||||
119 | for your object and reimplement the QObject::timerEvent() event | - | ||||||||||||||||||
120 | handler in your class (which must inherit QObject). The | - | ||||||||||||||||||
121 | disadvantage is that timerEvent() does not support such | - | ||||||||||||||||||
122 | high-level features as single-shot timers or signals. | - | ||||||||||||||||||
123 | - | |||||||||||||||||||
124 | Another alternative is QBasicTimer. It is typically less | - | ||||||||||||||||||
125 | cumbersome than using QObject::startTimer() | - | ||||||||||||||||||
126 | directly. See \l{Timers} for an overview of all three approaches. | - | ||||||||||||||||||
127 | - | |||||||||||||||||||
128 | Some operating systems limit the number of timers that may be | - | ||||||||||||||||||
129 | used; Qt tries to work around these limitations. | - | ||||||||||||||||||
130 | - | |||||||||||||||||||
131 | \sa QBasicTimer, QTimerEvent, QObject::timerEvent(), Timers, | - | ||||||||||||||||||
132 | {Analog Clock Example}, {Wiggly Example} | - | ||||||||||||||||||
133 | */ | - | ||||||||||||||||||
134 | - | |||||||||||||||||||
135 | static const int INV_TIMER = -1; // invalid timer id | - | ||||||||||||||||||
136 | - | |||||||||||||||||||
137 | /*! | - | ||||||||||||||||||
138 | Constructs a timer with the given \a parent. | - | ||||||||||||||||||
139 | */ | - | ||||||||||||||||||
140 | - | |||||||||||||||||||
141 | QTimer::QTimer(QObject *parent) | - | ||||||||||||||||||
142 | : QObject(parent), id(INV_TIMER), inter(0), del(0), single(0), nulltimer(0), type(Qt::CoarseTimer) | - | ||||||||||||||||||
143 | { | - | ||||||||||||||||||
144 | } executed 5467 times by 65 tests: end of block Executed by:
| 5467 | ||||||||||||||||||
145 | - | |||||||||||||||||||
146 | - | |||||||||||||||||||
147 | /*! | - | ||||||||||||||||||
148 | Destroys the timer. | - | ||||||||||||||||||
149 | */ | - | ||||||||||||||||||
150 | - | |||||||||||||||||||
151 | QTimer::~QTimer() | - | ||||||||||||||||||
152 | { | - | ||||||||||||||||||
153 | if (id != INV_TIMER) // stop running timer
| 318-5136 | ||||||||||||||||||
154 | stop(); executed 318 times by 40 tests: stop(); Executed by:
| 318 | ||||||||||||||||||
155 | } executed 5454 times by 79 tests: end of block Executed by:
| 5454 | ||||||||||||||||||
156 | - | |||||||||||||||||||
157 | - | |||||||||||||||||||
158 | /*! | - | ||||||||||||||||||
159 | \fn void QTimer::timeout() | - | ||||||||||||||||||
160 | - | |||||||||||||||||||
161 | This signal is emitted when the timer times out. | - | ||||||||||||||||||
162 | - | |||||||||||||||||||
163 | \sa interval, start(), stop() | - | ||||||||||||||||||
164 | */ | - | ||||||||||||||||||
165 | - | |||||||||||||||||||
166 | /*! | - | ||||||||||||||||||
167 | \property QTimer::active | - | ||||||||||||||||||
168 | \since 4.3 | - | ||||||||||||||||||
169 | - | |||||||||||||||||||
170 | This boolean property is \c true if the timer is running; otherwise | - | ||||||||||||||||||
171 | false. | - | ||||||||||||||||||
172 | */ | - | ||||||||||||||||||
173 | - | |||||||||||||||||||
174 | /*! | - | ||||||||||||||||||
175 | \fn bool QTimer::isActive() const | - | ||||||||||||||||||
176 | - | |||||||||||||||||||
177 | Returns \c true if the timer is running (pending); otherwise returns | - | ||||||||||||||||||
178 | false. | - | ||||||||||||||||||
179 | */ | - | ||||||||||||||||||
180 | - | |||||||||||||||||||
181 | /*! | - | ||||||||||||||||||
182 | \fn int QTimer::timerId() const | - | ||||||||||||||||||
183 | - | |||||||||||||||||||
184 | Returns the ID of the timer if the timer is running; otherwise returns | - | ||||||||||||||||||
185 | -1. | - | ||||||||||||||||||
186 | */ | - | ||||||||||||||||||
187 | - | |||||||||||||||||||
188 | - | |||||||||||||||||||
189 | /*! \overload start() | - | ||||||||||||||||||
190 | - | |||||||||||||||||||
191 | Starts or restarts the timer with the timeout specified in \l interval. | - | ||||||||||||||||||
192 | - | |||||||||||||||||||
193 | If the timer is already running, it will be | - | ||||||||||||||||||
194 | \l{QTimer::stop()}{stopped} and restarted. | - | ||||||||||||||||||
195 | - | |||||||||||||||||||
196 | If \l singleShot is true, the timer will be activated only once. | - | ||||||||||||||||||
197 | */ | - | ||||||||||||||||||
198 | void QTimer::start() | - | ||||||||||||||||||
199 | { | - | ||||||||||||||||||
200 | if (id != INV_TIMER) // stop running timer
| 22-5231 | ||||||||||||||||||
201 | stop(); executed 22 times by 2 tests: stop(); Executed by:
| 22 | ||||||||||||||||||
202 | nulltimer = (!inter && single);
| 9-4871 | ||||||||||||||||||
203 | id = QObject::startTimer(inter, Qt::TimerType(type)); | - | ||||||||||||||||||
204 | } executed 5253 times by 57 tests: end of block Executed by:
| 5253 | ||||||||||||||||||
205 | - | |||||||||||||||||||
206 | /*! | - | ||||||||||||||||||
207 | Starts or restarts the timer with a timeout interval of \a msec | - | ||||||||||||||||||
208 | milliseconds. | - | ||||||||||||||||||
209 | - | |||||||||||||||||||
210 | If the timer is already running, it will be | - | ||||||||||||||||||
211 | \l{QTimer::stop()}{stopped} and restarted. | - | ||||||||||||||||||
212 | - | |||||||||||||||||||
213 | If \l singleShot is true, the timer will be activated only once. | - | ||||||||||||||||||
214 | - | |||||||||||||||||||
215 | */ | - | ||||||||||||||||||
216 | void QTimer::start(int msec) | - | ||||||||||||||||||
217 | { | - | ||||||||||||||||||
218 | inter = msec; | - | ||||||||||||||||||
219 | start(); | - | ||||||||||||||||||
220 | } executed 5131 times by 46 tests: end of block Executed by:
| 5131 | ||||||||||||||||||
221 | - | |||||||||||||||||||
222 | - | |||||||||||||||||||
223 | - | |||||||||||||||||||
224 | /*! | - | ||||||||||||||||||
225 | Stops the timer. | - | ||||||||||||||||||
226 | - | |||||||||||||||||||
227 | \sa start() | - | ||||||||||||||||||
228 | */ | - | ||||||||||||||||||
229 | - | |||||||||||||||||||
230 | void QTimer::stop() | - | ||||||||||||||||||
231 | { | - | ||||||||||||||||||
232 | if (id != INV_TIMER) {
| 5253-5611 | ||||||||||||||||||
233 | QObject::killTimer(id); | - | ||||||||||||||||||
234 | id = INV_TIMER; | - | ||||||||||||||||||
235 | } executed 5253 times by 68 tests: end of block Executed by:
| 5253 | ||||||||||||||||||
236 | } executed 10864 times by 69 tests: end of block Executed by:
| 10864 | ||||||||||||||||||
237 | - | |||||||||||||||||||
238 | - | |||||||||||||||||||
239 | /*! | - | ||||||||||||||||||
240 | \reimp | - | ||||||||||||||||||
241 | */ | - | ||||||||||||||||||
242 | void QTimer::timerEvent(QTimerEvent *e) | - | ||||||||||||||||||
243 | { | - | ||||||||||||||||||
244 | if (e->timerId() == id) {
| 0-13215 | ||||||||||||||||||
245 | if (single)
| 390-12825 | ||||||||||||||||||
246 | stop(); executed 390 times by 19 tests: stop(); Executed by:
| 390 | ||||||||||||||||||
247 | emit timeout(QPrivateSignal()); | - | ||||||||||||||||||
248 | } executed 13214 times by 31 tests: end of block Executed by:
| 13214 | ||||||||||||||||||
249 | } executed 13214 times by 31 tests: end of block Executed by:
| 13214 | ||||||||||||||||||
250 | - | |||||||||||||||||||
251 | class QSingleShotTimer : public QObject | - | ||||||||||||||||||
252 | { | - | ||||||||||||||||||
253 | Q_OBJECT | - | ||||||||||||||||||
254 | int timerId; | - | ||||||||||||||||||
255 | bool hasValidReceiver; | - | ||||||||||||||||||
256 | QPointer<const QObject> receiver; | - | ||||||||||||||||||
257 | QtPrivate::QSlotObjectBase *slotObj; | - | ||||||||||||||||||
258 | public: | - | ||||||||||||||||||
259 | ~QSingleShotTimer(); | - | ||||||||||||||||||
260 | QSingleShotTimer(int msec, Qt::TimerType timerType, const QObject *r, const char * m); | - | ||||||||||||||||||
261 | QSingleShotTimer(int msec, Qt::TimerType timerType, const QObject *r, QtPrivate::QSlotObjectBase *slotObj); | - | ||||||||||||||||||
262 | - | |||||||||||||||||||
263 | Q_SIGNALS: | - | ||||||||||||||||||
264 | void timeout(); | - | ||||||||||||||||||
265 | protected: | - | ||||||||||||||||||
266 | void timerEvent(QTimerEvent *) Q_DECL_OVERRIDE; | - | ||||||||||||||||||
267 | }; | - | ||||||||||||||||||
268 | - | |||||||||||||||||||
269 | QSingleShotTimer::QSingleShotTimer(int msec, Qt::TimerType timerType, const QObject *r, const char *member) | - | ||||||||||||||||||
270 | : QObject(QAbstractEventDispatcher::instance()), hasValidReceiver(true), slotObj(0) | - | ||||||||||||||||||
271 | { | - | ||||||||||||||||||
272 | timerId = startTimer(msec, timerType); | - | ||||||||||||||||||
273 | connect(this, SIGNAL(timeout()), r, member); | - | ||||||||||||||||||
274 | } executed 1258 times by 33 tests: end of block Executed by:
| 1258 | ||||||||||||||||||
275 | - | |||||||||||||||||||
276 | QSingleShotTimer::QSingleShotTimer(int msec, Qt::TimerType timerType, const QObject *r, QtPrivate::QSlotObjectBase *slotObj) | - | ||||||||||||||||||
277 | : QObject(QAbstractEventDispatcher::instance()), hasValidReceiver(r), receiver(r), slotObj(slotObj) | - | ||||||||||||||||||
278 | { | - | ||||||||||||||||||
279 | timerId = startTimer(msec, timerType); | - | ||||||||||||||||||
280 | if (r && thread() != r->thread()) {
| 5-20011 | ||||||||||||||||||
281 | // Avoid leaking the QSingleShotTimer instance in case the application exits before the timer fires | - | ||||||||||||||||||
282 | connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, this, &QObject::deleteLater); | - | ||||||||||||||||||
283 | setParent(0); | - | ||||||||||||||||||
284 | moveToThread(r->thread()); | - | ||||||||||||||||||
285 | } executed 20006 times by 1 test: end of block Executed by:
| 20006 | ||||||||||||||||||
286 | } executed 20017 times by 2 tests: end of block Executed by:
| 20017 | ||||||||||||||||||
287 | - | |||||||||||||||||||
288 | QSingleShotTimer::~QSingleShotTimer() | - | ||||||||||||||||||
289 | { | - | ||||||||||||||||||
290 | if (timerId > 0)
| 138-21132 | ||||||||||||||||||
291 | killTimer(timerId); executed 138 times by 10 tests: killTimer(timerId); Executed by:
| 138 | ||||||||||||||||||
292 | if (slotObj)
| 1253-20017 | ||||||||||||||||||
293 | slotObj->destroyIfLastRef(); executed 20017 times by 2 tests: slotObj->destroyIfLastRef(); Executed by:
| 20017 | ||||||||||||||||||
294 | } executed 21270 times by 38 tests: end of block Executed by:
| 21270 | ||||||||||||||||||
295 | - | |||||||||||||||||||
296 | void QSingleShotTimer::timerEvent(QTimerEvent *) | - | ||||||||||||||||||
297 | { | - | ||||||||||||||||||
298 | // need to kill the timer _before_ we emit timeout() in case the | - | ||||||||||||||||||
299 | // slot connected to timeout calls processEvents() | - | ||||||||||||||||||
300 | if (timerId > 0)
| 0-21132 | ||||||||||||||||||
301 | killTimer(timerId); executed 21132 times by 32 tests: killTimer(timerId); Executed by:
| 21132 | ||||||||||||||||||
302 | timerId = -1; | - | ||||||||||||||||||
303 | - | |||||||||||||||||||
304 | if (slotObj) {
| 1115-20017 | ||||||||||||||||||
305 | // If the receiver was destroyed, skip this part | - | ||||||||||||||||||
306 | if (Q_LIKELY(!receiver.isNull() || !hasValidReceiver)) {
| 2-20015 | ||||||||||||||||||
307 | // We allocate only the return type - we previously checked the function had | - | ||||||||||||||||||
308 | // no arguments. | - | ||||||||||||||||||
309 | void *args[1] = { 0 }; | - | ||||||||||||||||||
310 | slotObj->call(const_cast<QObject*>(receiver.data()), args); | - | ||||||||||||||||||
311 | } executed 20015 times by 2 tests: end of block Executed by:
| 20015 | ||||||||||||||||||
312 | } else { executed 20017 times by 2 tests: end of block Executed by:
| 20017 | ||||||||||||||||||
313 | emit timeout(); | - | ||||||||||||||||||
314 | } executed 1115 times by 31 tests: end of block Executed by:
| 1115 | ||||||||||||||||||
315 | - | |||||||||||||||||||
316 | // we would like to use delete later here, but it feels like a | - | ||||||||||||||||||
317 | // waste to post a new event to handle this event, so we just unset the flag | - | ||||||||||||||||||
318 | // and explicitly delete... | - | ||||||||||||||||||
319 | qDeleteInEventHandler(this); | - | ||||||||||||||||||
320 | } executed 21132 times by 32 tests: end of block Executed by:
| 21132 | ||||||||||||||||||
321 | - | |||||||||||||||||||
322 | /*! | - | ||||||||||||||||||
323 | \internal | - | ||||||||||||||||||
324 | - | |||||||||||||||||||
325 | Implementation of the template version of singleShot | - | ||||||||||||||||||
326 | - | |||||||||||||||||||
327 | \a msec is the timer interval | - | ||||||||||||||||||
328 | \a timerType is the timer type | - | ||||||||||||||||||
329 | \a receiver is the receiver object, can be null. In such a case, it will be the same | - | ||||||||||||||||||
330 | as the final sender class. | - | ||||||||||||||||||
331 | \a slot a pointer only used when using Qt::UniqueConnection | - | ||||||||||||||||||
332 | \a slotObj the slot object | - | ||||||||||||||||||
333 | */ | - | ||||||||||||||||||
334 | void QTimer::singleShotImpl(int msec, Qt::TimerType timerType, | - | ||||||||||||||||||
335 | const QObject *receiver, | - | ||||||||||||||||||
336 | QtPrivate::QSlotObjectBase *slotObj) | - | ||||||||||||||||||
337 | { | - | ||||||||||||||||||
338 | new QSingleShotTimer(msec, timerType, receiver, slotObj); | - | ||||||||||||||||||
339 | } executed 20017 times by 2 tests: end of block Executed by:
| 20017 | ||||||||||||||||||
340 | - | |||||||||||||||||||
341 | /*! | - | ||||||||||||||||||
342 | \reentrant | - | ||||||||||||||||||
343 | This static function calls a slot after a given time interval. | - | ||||||||||||||||||
344 | - | |||||||||||||||||||
345 | It is very convenient to use this function because you do not need | - | ||||||||||||||||||
346 | to bother with a \l{QObject::timerEvent()}{timerEvent} or | - | ||||||||||||||||||
347 | create a local QTimer object. | - | ||||||||||||||||||
348 | - | |||||||||||||||||||
349 | Example: | - | ||||||||||||||||||
350 | \snippet code/src_corelib_kernel_qtimer.cpp 0 | - | ||||||||||||||||||
351 | - | |||||||||||||||||||
352 | This sample program automatically terminates after 10 minutes | - | ||||||||||||||||||
353 | (600,000 milliseconds). | - | ||||||||||||||||||
354 | - | |||||||||||||||||||
355 | The \a receiver is the receiving object and the \a member is the | - | ||||||||||||||||||
356 | slot. The time interval is \a msec milliseconds. | - | ||||||||||||||||||
357 | - | |||||||||||||||||||
358 | \sa start() | - | ||||||||||||||||||
359 | */ | - | ||||||||||||||||||
360 | - | |||||||||||||||||||
361 | void QTimer::singleShot(int msec, const QObject *receiver, const char *member) | - | ||||||||||||||||||
362 | { | - | ||||||||||||||||||
363 | // coarse timers are worst in their first firing | - | ||||||||||||||||||
364 | // so we prefer a high precision timer for something that happens only once | - | ||||||||||||||||||
365 | // unless the timeout is too big, in which case we go for coarse anyway | - | ||||||||||||||||||
366 | singleShot(msec, msec >= 2000 ? Qt::CoarseTimer : Qt::PreciseTimer, receiver, member); | - | ||||||||||||||||||
367 | } executed 4558 times by 173 tests: end of block Executed by:
| 4558 | ||||||||||||||||||
368 | - | |||||||||||||||||||
369 | /*! \overload | - | ||||||||||||||||||
370 | \reentrant | - | ||||||||||||||||||
371 | This static function calls a slot after a given time interval. | - | ||||||||||||||||||
372 | - | |||||||||||||||||||
373 | It is very convenient to use this function because you do not need | - | ||||||||||||||||||
374 | to bother with a \l{QObject::timerEvent()}{timerEvent} or | - | ||||||||||||||||||
375 | create a local QTimer object. | - | ||||||||||||||||||
376 | - | |||||||||||||||||||
377 | The \a receiver is the receiving object and the \a member is the slot. The | - | ||||||||||||||||||
378 | time interval is \a msec milliseconds. The \a timerType affects the | - | ||||||||||||||||||
379 | accuracy of the timer. | - | ||||||||||||||||||
380 | - | |||||||||||||||||||
381 | \sa start() | - | ||||||||||||||||||
382 | */ | - | ||||||||||||||||||
383 | void QTimer::singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, const char *member) | - | ||||||||||||||||||
384 | { | - | ||||||||||||||||||
385 | if (Q_UNLIKELY(msec < 0)) {
| 1-4557 | ||||||||||||||||||
386 | qWarning("QTimer::singleShot: Timers cannot have negative timeouts"); | - | ||||||||||||||||||
387 | return; executed 1 time by 1 test: return; Executed by:
| 1 | ||||||||||||||||||
388 | } | - | ||||||||||||||||||
389 | if (receiver && member) {
| 0-4557 | ||||||||||||||||||
390 | if (msec == 0) {
| 1258-3299 | ||||||||||||||||||
391 | // special code shortpath for 0-timers | - | ||||||||||||||||||
392 | const char* bracketPosition = strchr(member, '('); | - | ||||||||||||||||||
393 | if (!bracketPosition || !(member[0] >= '0' && member[0] <= '2')) {
| 0-3299 | ||||||||||||||||||
394 | qWarning("QTimer::singleShot: Invalid slot specification"); | - | ||||||||||||||||||
395 | return; never executed: return; | 0 | ||||||||||||||||||
396 | } | - | ||||||||||||||||||
397 | QByteArray methodName(member+1, bracketPosition - 1 - member); // extract method name | - | ||||||||||||||||||
398 | QMetaObject::invokeMethod(const_cast<QObject *>(receiver), methodName.constData(), Qt::QueuedConnection); | - | ||||||||||||||||||
399 | return; executed 3299 times by 168 tests: return; Executed by:
| 3299 | ||||||||||||||||||
400 | } | - | ||||||||||||||||||
401 | (void) new QSingleShotTimer(msec, timerType, receiver, member); | - | ||||||||||||||||||
402 | } executed 1258 times by 33 tests: end of block Executed by:
| 1258 | ||||||||||||||||||
403 | } executed 1258 times by 33 tests: end of block Executed by:
| 1258 | ||||||||||||||||||
404 | - | |||||||||||||||||||
405 | /*!\fn void QTimer::singleShot(int msec, const QObject *receiver, PointerToMemberFunction method) | - | ||||||||||||||||||
406 | - | |||||||||||||||||||
407 | \since 5.4 | - | ||||||||||||||||||
408 | - | |||||||||||||||||||
409 | \overload | - | ||||||||||||||||||
410 | \reentrant | - | ||||||||||||||||||
411 | This static function calls a member function of a QObject after a given time interval. | - | ||||||||||||||||||
412 | - | |||||||||||||||||||
413 | It is very convenient to use this function because you do not need | - | ||||||||||||||||||
414 | to bother with a \l{QObject::timerEvent()}{timerEvent} or | - | ||||||||||||||||||
415 | create a local QTimer object. | - | ||||||||||||||||||
416 | - | |||||||||||||||||||
417 | The \a receiver is the receiving object and the \a method is the member function. The | - | ||||||||||||||||||
418 | time interval is \a msec milliseconds. | - | ||||||||||||||||||
419 | - | |||||||||||||||||||
420 | If \a receiver is destroyed before the interval occurs, the method will not be called. | - | ||||||||||||||||||
421 | The function will be run in the thread of \a receiver. The receiver's thread must have | - | ||||||||||||||||||
422 | a running Qt event loop. | - | ||||||||||||||||||
423 | - | |||||||||||||||||||
424 | \sa start() | - | ||||||||||||||||||
425 | */ | - | ||||||||||||||||||
426 | - | |||||||||||||||||||
427 | /*!\fn void QTimer::singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, PointerToMemberFunction method) | - | ||||||||||||||||||
428 | - | |||||||||||||||||||
429 | \since 5.4 | - | ||||||||||||||||||
430 | - | |||||||||||||||||||
431 | \overload | - | ||||||||||||||||||
432 | \reentrant | - | ||||||||||||||||||
433 | This static function calls a member function of a QObject after a given time interval. | - | ||||||||||||||||||
434 | - | |||||||||||||||||||
435 | It is very convenient to use this function because you do not need | - | ||||||||||||||||||
436 | to bother with a \l{QObject::timerEvent()}{timerEvent} or | - | ||||||||||||||||||
437 | create a local QTimer object. | - | ||||||||||||||||||
438 | - | |||||||||||||||||||
439 | The \a receiver is the receiving object and the \a method is the member function. The | - | ||||||||||||||||||
440 | time interval is \a msec milliseconds. The \a timerType affects the | - | ||||||||||||||||||
441 | accuracy of the timer. | - | ||||||||||||||||||
442 | - | |||||||||||||||||||
443 | If \a receiver is destroyed before the interval occurs, the method will not be called. | - | ||||||||||||||||||
444 | The function will be run in the thread of \a receiver. The receiver's thread must have | - | ||||||||||||||||||
445 | a running Qt event loop. | - | ||||||||||||||||||
446 | - | |||||||||||||||||||
447 | \sa start() | - | ||||||||||||||||||
448 | */ | - | ||||||||||||||||||
449 | - | |||||||||||||||||||
450 | /*!\fn void QTimer::singleShot(int msec, Functor functor) | - | ||||||||||||||||||
451 | - | |||||||||||||||||||
452 | \since 5.4 | - | ||||||||||||||||||
453 | - | |||||||||||||||||||
454 | \overload | - | ||||||||||||||||||
455 | \reentrant | - | ||||||||||||||||||
456 | This static function calls \a functor after a given time interval. | - | ||||||||||||||||||
457 | - | |||||||||||||||||||
458 | It is very convenient to use this function because you do not need | - | ||||||||||||||||||
459 | to bother with a \l{QObject::timerEvent()}{timerEvent} or | - | ||||||||||||||||||
460 | create a local QTimer object. | - | ||||||||||||||||||
461 | - | |||||||||||||||||||
462 | The time interval is \a msec milliseconds. | - | ||||||||||||||||||
463 | - | |||||||||||||||||||
464 | \sa start() | - | ||||||||||||||||||
465 | */ | - | ||||||||||||||||||
466 | - | |||||||||||||||||||
467 | /*!\fn void QTimer::singleShot(int msec, Qt::TimerType timerType, Functor functor) | - | ||||||||||||||||||
468 | - | |||||||||||||||||||
469 | \since 5.4 | - | ||||||||||||||||||
470 | - | |||||||||||||||||||
471 | \overload | - | ||||||||||||||||||
472 | \reentrant | - | ||||||||||||||||||
473 | This static function calls \a functor after a given time interval. | - | ||||||||||||||||||
474 | - | |||||||||||||||||||
475 | It is very convenient to use this function because you do not need | - | ||||||||||||||||||
476 | to bother with a \l{QObject::timerEvent()}{timerEvent} or | - | ||||||||||||||||||
477 | create a local QTimer object. | - | ||||||||||||||||||
478 | - | |||||||||||||||||||
479 | The time interval is \a msec milliseconds. The \a timerType affects the | - | ||||||||||||||||||
480 | accuracy of the timer. | - | ||||||||||||||||||
481 | - | |||||||||||||||||||
482 | \sa start() | - | ||||||||||||||||||
483 | */ | - | ||||||||||||||||||
484 | - | |||||||||||||||||||
485 | /*!\fn void QTimer::singleShot(int msec, const QObject *context, Functor functor) | - | ||||||||||||||||||
486 | - | |||||||||||||||||||
487 | \since 5.4 | - | ||||||||||||||||||
488 | - | |||||||||||||||||||
489 | \overload | - | ||||||||||||||||||
490 | \reentrant | - | ||||||||||||||||||
491 | This static function calls \a functor after a given time interval. | - | ||||||||||||||||||
492 | - | |||||||||||||||||||
493 | It is very convenient to use this function because you do not need | - | ||||||||||||||||||
494 | to bother with a \l{QObject::timerEvent()}{timerEvent} or | - | ||||||||||||||||||
495 | create a local QTimer object. | - | ||||||||||||||||||
496 | - | |||||||||||||||||||
497 | The time interval is \a msec milliseconds. | - | ||||||||||||||||||
498 | - | |||||||||||||||||||
499 | If \a context is destroyed before the interval occurs, the method will not be called. | - | ||||||||||||||||||
500 | The function will be run in the thread of \a context. The context's thread must have | - | ||||||||||||||||||
501 | a running Qt event loop. | - | ||||||||||||||||||
502 | - | |||||||||||||||||||
503 | \sa start() | - | ||||||||||||||||||
504 | */ | - | ||||||||||||||||||
505 | - | |||||||||||||||||||
506 | /*!\fn void QTimer::singleShot(int msec, Qt::TimerType timerType, const QObject *context, Functor functor) | - | ||||||||||||||||||
507 | - | |||||||||||||||||||
508 | \since 5.4 | - | ||||||||||||||||||
509 | - | |||||||||||||||||||
510 | \overload | - | ||||||||||||||||||
511 | \reentrant | - | ||||||||||||||||||
512 | This static function calls \a functor after a given time interval. | - | ||||||||||||||||||
513 | - | |||||||||||||||||||
514 | It is very convenient to use this function because you do not need | - | ||||||||||||||||||
515 | to bother with a \l{QObject::timerEvent()}{timerEvent} or | - | ||||||||||||||||||
516 | create a local QTimer object. | - | ||||||||||||||||||
517 | - | |||||||||||||||||||
518 | The time interval is \a msec milliseconds. The \a timerType affects the | - | ||||||||||||||||||
519 | accuracy of the timer. | - | ||||||||||||||||||
520 | - | |||||||||||||||||||
521 | If \a context is destroyed before the interval occurs, the method will not be called. | - | ||||||||||||||||||
522 | The function will be run in the thread of \a context. The context's thread must have | - | ||||||||||||||||||
523 | a running Qt event loop. | - | ||||||||||||||||||
524 | - | |||||||||||||||||||
525 | \sa start() | - | ||||||||||||||||||
526 | */ | - | ||||||||||||||||||
527 | - | |||||||||||||||||||
528 | /*! | - | ||||||||||||||||||
529 | \property QTimer::singleShot | - | ||||||||||||||||||
530 | \brief whether the timer is a single-shot timer | - | ||||||||||||||||||
531 | - | |||||||||||||||||||
532 | A single-shot timer fires only once, non-single-shot timers fire | - | ||||||||||||||||||
533 | every \l interval milliseconds. | - | ||||||||||||||||||
534 | - | |||||||||||||||||||
535 | The default value for this property is \c false. | - | ||||||||||||||||||
536 | - | |||||||||||||||||||
537 | \sa interval, singleShot() | - | ||||||||||||||||||
538 | */ | - | ||||||||||||||||||
539 | - | |||||||||||||||||||
540 | /*! | - | ||||||||||||||||||
541 | \property QTimer::interval | - | ||||||||||||||||||
542 | \brief the timeout interval in milliseconds | - | ||||||||||||||||||
543 | - | |||||||||||||||||||
544 | The default value for this property is 0. A QTimer with a timeout | - | ||||||||||||||||||
545 | interval of 0 will time out as soon as all the events in the window | - | ||||||||||||||||||
546 | system's event queue have been processed. | - | ||||||||||||||||||
547 | - | |||||||||||||||||||
548 | Setting the interval of an active timer changes its timerId(). | - | ||||||||||||||||||
549 | - | |||||||||||||||||||
550 | \sa singleShot | - | ||||||||||||||||||
551 | */ | - | ||||||||||||||||||
552 | void QTimer::setInterval(int msec) | - | ||||||||||||||||||
553 | { | - | ||||||||||||||||||
554 | inter = msec; | - | ||||||||||||||||||
555 | if (id != INV_TIMER) { // create new timer
| 0-71 | ||||||||||||||||||
556 | QObject::killTimer(id); // restart timer | - | ||||||||||||||||||
557 | id = QObject::startTimer(msec, Qt::TimerType(type)); | - | ||||||||||||||||||
558 | } never executed: end of block | 0 | ||||||||||||||||||
559 | } executed 71 times by 34 tests: end of block Executed by:
| 71 | ||||||||||||||||||
560 | - | |||||||||||||||||||
561 | /*! | - | ||||||||||||||||||
562 | \property QTimer::remainingTime | - | ||||||||||||||||||
563 | \since 5.0 | - | ||||||||||||||||||
564 | \brief the remaining time in milliseconds | - | ||||||||||||||||||
565 | - | |||||||||||||||||||
566 | Returns the timer's remaining value in milliseconds left until the timeout. | - | ||||||||||||||||||
567 | If the timer is inactive, the returned value will be -1. If the timer is | - | ||||||||||||||||||
568 | overdue, the returned value will be 0. | - | ||||||||||||||||||
569 | - | |||||||||||||||||||
570 | \sa interval | - | ||||||||||||||||||
571 | */ | - | ||||||||||||||||||
572 | int QTimer::remainingTime() const | - | ||||||||||||||||||
573 | { | - | ||||||||||||||||||
574 | if (id != INV_TIMER) {
| 4 | ||||||||||||||||||
575 | return QAbstractEventDispatcher::instance()->remainingTime(id); executed 4 times by 1 test: return QAbstractEventDispatcher::instance()->remainingTime(id); Executed by:
| 4 | ||||||||||||||||||
576 | } | - | ||||||||||||||||||
577 | - | |||||||||||||||||||
578 | return -1; executed 4 times by 1 test: return -1; Executed by:
| 4 | ||||||||||||||||||
579 | } | - | ||||||||||||||||||
580 | - | |||||||||||||||||||
581 | /*! | - | ||||||||||||||||||
582 | \property QTimer::timerType | - | ||||||||||||||||||
583 | \brief controls the accuracy of the timer | - | ||||||||||||||||||
584 | - | |||||||||||||||||||
585 | The default value for this property is \c Qt::CoarseTimer. | - | ||||||||||||||||||
586 | - | |||||||||||||||||||
587 | \sa Qt::TimerType | - | ||||||||||||||||||
588 | */ | - | ||||||||||||||||||
589 | - | |||||||||||||||||||
590 | QT_END_NAMESPACE | - | ||||||||||||||||||
591 | - | |||||||||||||||||||
592 | #include "qtimer.moc" | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |