diff options
| author | Ahmad Samir <a.samirh78@gmail.com> | 2024-12-19 14:10:31 +0200 |
|---|---|---|
| committer | Ahmad Samir <a.samirh78@gmail.com> | 2025-02-10 01:13:29 +0200 |
| commit | f1f610bc67bfd5c2ef31270a6945e7bae93b5e4a (patch) | |
| tree | 95df313ad5a5a6bd79501073a6f7311a257d1a1b /src/corelib/kernel/qbasictimer.cpp | |
| parent | d4c312f68aee526848b7b2f0e4b77948e8b55e8f (diff) | |
Qt Timers: disallow setting negative intervals
As disccussed in the code review. Replace negative values with 1ms (not
0ms as that would spin up the event-loop too many times).
Move QTimer::start(std::chrono::milliseconds msec) API docs next to the
method definition so as to not forget changing it when the
implementation changes.
Drive-by, qWarning() and co. are marked cold so there is no need to use
Q_UNLIKELY in an if-condition leading to calling these methods.
[ChangeLog][QtCore][Important Behvaior Change] Timers with negative
intervals aren't allowed anymore, that is, if you try to start a timer
(or set the interval) with a negative value, that interval will be set
to 1ms. Previously Qt timers would let you set the interval to a
negative value, but behave in surprising ways (for example stop the
timer if it was running or not start it at all). This change affects
QTimer, QChronoTimer, QBasicTimer and QObject::startTimer().
Fixes: QTBUG-132359
Change-Id: I09309063665db051337f91160971993d9ce7911e
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/kernel/qbasictimer.cpp')
| -rw-r--r-- | src/corelib/kernel/qbasictimer.cpp | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/src/corelib/kernel/qbasictimer.cpp b/src/corelib/kernel/qbasictimer.cpp index b7e2f114519..60387381da6 100644 --- a/src/corelib/kernel/qbasictimer.cpp +++ b/src/corelib/kernel/qbasictimer.cpp @@ -145,6 +145,8 @@ QT_BEGIN_NAMESPACE The given \a object will receive timer events. + \include timers-common.qdocinc negative-intervals-not-allowed + //! [start-nanoseconds-note] \note Starting from Qt 6.9 this method takes std::chrono::nanoseconds, before that it took std::chrono::milliseconds. This change is @@ -170,6 +172,8 @@ QT_BEGIN_NAMESPACE given \a timerType. See Qt::TimerType for information on the different timer types. + \include timers-common.qdocinc negative-intervals-not-allowed + \a obj will receive timer events. \include qbasictimer.cpp start-nanoseconds-note @@ -179,9 +183,10 @@ QT_BEGIN_NAMESPACE void QBasicTimer::start(Duration duration, Qt::TimerType timerType, QObject *obj) { QAbstractEventDispatcher *eventDispatcher = QAbstractEventDispatcher::instance(); - if (Q_UNLIKELY(duration < 0ns)) { - qWarning("QBasicTimer::start: Timers cannot have negative timeouts"); - return; + if (duration < 0ns) { + qWarning("QBasicTimer::start: negative intervals aren't allowed; the " + "interval will be set to 1ms."); + duration = 1ms; } if (Q_UNLIKELY(!eventDispatcher)) { qWarning("QBasicTimer::start: QBasicTimer can only be used with threads started with QThread"); |
