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