| 1 |
// Copyright (C) 2024 Ahmad Samir <[email protected]> |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only |
| 3 |
|
| 4 |
//! [q-chrono-timer-alternatives] |
| 5 |
Another alternative is reimplementing the QObject::timerEvent() method |
| 6 |
in your class (which must be a sub-class of QObject), and using one of |
| 7 |
the following approaches: |
| 8 |
|
| 9 |
\list |
| 10 |
\li Using QBasicTimer, a lightweight value-class wrapping a timer |
| 11 |
ID. You can start the timer with QBasicTimer::start() and stop it with |
| 12 |
QBasicTimer::stop(). You can handle the event in your reimplemneted |
| 13 |
timerEvent(). |
| 14 |
|
| 15 |
\li A more low-level method is manipulating the timer IDs directly. |
| 16 |
To start the timer call QObject::startTimer(), storing the returned |
| 17 |
ID. To stop the timer call QObject::killTimer(). You can handle the event |
| 18 |
in your reimplemented timerEvent(). This approach is typically more |
| 19 |
cumbersome than using QBasicTimer. |
| 20 |
\endlist |
| 21 |
|
| 22 |
A disadvantage of using timerEvent() is that some high-level features, |
| 23 |
such as single-shot timers and signals, aren't supported. |
| 24 |
//! [q-chrono-timer-alternatives] |
| 25 |
|
| 26 |
// \1 is the method argument's name e.g. `\a interval` |
| 27 |
//! [negative-intervals-not-allowed] |
| 28 |
Starting from Qt 6.10, setting a negative interval will result in a run-time warning |
| 29 |
and the value being reset to 1ms. Before Qt 6.10 a Qt Timer would let you set a negative |
| 30 |
interval but behave in surprising ways (for example stop the timer if it was running or |
| 31 |
not start it at all). |
| 32 |
//! [negative-intervals-not-allowed] |