diff options
author | Ahmad Samir <a.samirh78@gmail.com> | 2023-11-18 21:29:26 +0200 |
---|---|---|
committer | Ahmad Samir <a.samirh78@gmail.com> | 2024-03-03 19:56:55 +0200 |
commit | 4bc0834bc182335984431c6a1525782efc34368c (patch) | |
tree | 87db305cc2d9e0995cf46225b3a5d6dee93c9de3 /src/corelib/kernel/qobject.cpp | |
parent | 577a3dba521f7f69bf6129fcd28184ae288182d9 (diff) |
Timers: add Qt::TimerId enum class
Which will be used to represent timer IDs. Thanks to Marc for the idea
to use "a strongly typed int".
QTimer got a new id() method that returns Qt::TimerId (can't overload
timerId()). Various classes in qtbase have a member named timerId(), but
a new method is needed anyway in QTimer so id() it is (this is the
reason QChronoTimer only has id() and no timerId()). Besides
timer.timerId() has an extra "timer".
This commit fixes the inconsistency between QObject using `0` timer id
to indicate "failed to start", while QTimer::timerId() returned `-1` to
indicate "timer is inactive". QTimer::id(), being a new method and all,
now returns Qt::TimerId::Invalid, which has value `0`, so that the
values match between the two classes. Extend the unittests to ensure
QTimer::timerId()'s behavior is preserved.
[ChangeLog][Core][QObject] Added Qt::TimerId enum class, that is used to
represent timer IDs.
Change-Id: I0e8564c1461884106d8a797cc980a669035d480a
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/kernel/qobject.cpp')
-rw-r--r-- | src/corelib/kernel/qobject.cpp | 29 |
1 files changed, 22 insertions, 7 deletions
diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index cbdb95079b..4fea4d0d3a 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -191,8 +191,8 @@ QObjectPrivate::~QObjectPrivate() thisThreadData->eventDispatcher.loadRelaxed()->unregisterTimers(q_ptr); // release the timer ids back to the pool - for (int i = 0; i < extraData->runningTimers.size(); ++i) - QAbstractEventDispatcherPrivate::releaseTimerId(extraData->runningTimers.at(i)); + for (auto id : std::as_const(extraData->runningTimers)) + QAbstractEventDispatcherPrivate::releaseTimerId(id); } else { qWarning("QObject::~QObject: Timers cannot be stopped from another thread"); } @@ -1888,6 +1888,13 @@ int QObject::startTimer(int interval, Qt::TimerType timerType) is \c std::chrono::nanoseconds, prior to that it was \c std::chrono::milliseconds. This change is backwards compatible with older releases of Qt. + + \note In Qt 6.8, QObject was changed to use Qt::TimerId to represent timer + IDs. This method converts the TimerId to int for backwards compatibility + reasons, however you can use Qt::TimerId to check the value returned by + this method, for example: + \snippet code/src_corelib_kernel_qobject.cpp invalid-timer-id + */ int QObject::startTimer(std::chrono::nanoseconds interval, Qt::TimerType timerType) { @@ -1914,7 +1921,7 @@ int QObject::startTimer(std::chrono::nanoseconds interval, Qt::TimerType timerTy const auto msecs = std::chrono::ceil<std::chrono::milliseconds>(interval); int timerId = dispatcher->registerTimer(msecs.count(), timerType, this); d->ensureExtraData(); - d->extraData->runningTimers.append(timerId); + d->extraData->runningTimers.append(Qt::TimerId{timerId}); return timerId; } @@ -1929,17 +1936,26 @@ int QObject::startTimer(std::chrono::nanoseconds interval, Qt::TimerType timerTy void QObject::killTimer(int id) { + killTimer(Qt::TimerId{id}); +} + +/*! + \since 6.8 + \overload +*/ +void QObject::killTimer(Qt::TimerId id) +{ Q_D(QObject); if (Q_UNLIKELY(thread() != QThread::currentThread())) { qWarning("QObject::killTimer: Timers cannot be stopped from another thread"); return; } - if (id) { + if (id > Qt::TimerId::Invalid) { int at = d->extraData ? d->extraData->runningTimers.indexOf(id) : -1; if (at == -1) { // timer isn't owned by this object qWarning("QObject::killTimer(): Error: timer id %d is not valid for object %p (%s, %ls), timer has not been killed", - id, + qToUnderlying(id), this, metaObject()->className(), qUtf16Printable(objectName())); @@ -1948,14 +1964,13 @@ void QObject::killTimer(int id) auto thisThreadData = d->threadData.loadRelaxed(); if (thisThreadData->hasEventDispatcher()) - thisThreadData->eventDispatcher.loadRelaxed()->unregisterTimer(id); + thisThreadData->eventDispatcher.loadRelaxed()->unregisterTimer(qToUnderlying(id)); d->extraData->runningTimers.remove(at); QAbstractEventDispatcherPrivate::releaseTimerId(id); } } - /*! \fn QObject *QObject::parent() const |