diff --git a/src/corelib/kernel/qbasictimer.cpp b/src/corelib/kernel/qbasictimer.cpp index 17711a355e6..613c0e696d6 100644 --- a/src/corelib/kernel/qbasictimer.cpp +++ b/src/corelib/kernel/qbasictimer.cpp @@ -101,12 +101,24 @@ QT_BEGIN_NAMESPACE /*! \fn int QBasicTimer::timerId() const + \obsolete Returns the timer's ID. + In new code use id() instead. + \sa QTimerEvent::timerId() */ +/*! + \fn Qt::TimerId QBasicTimer::id() const + \since 6.8 + + Returns the timer's ID. + + \sa QTimerEvent::id() +*/ + /*! \fn void QBasicTimer::start(int msec, QObject *object) @@ -165,7 +177,7 @@ void QBasicTimer::start(std::chrono::milliseconds duration, Qt::TimerType timerT } stop(); if (obj) - id = int(eventDispatcher->registerTimer(duration, timerType, obj)); + m_id = eventDispatcher->registerTimer(duration, timerType, obj); } /*! @@ -175,15 +187,15 @@ void QBasicTimer::start(std::chrono::milliseconds duration, Qt::TimerType timerT */ void QBasicTimer::stop() { - if (id) { + if (isActive()) { QAbstractEventDispatcher *eventDispatcher = QAbstractEventDispatcher::instance(); - if (eventDispatcher && !eventDispatcher->unregisterTimer(Qt::TimerId(id))) { + if (eventDispatcher && !eventDispatcher->unregisterTimer(m_id)) { qWarning("QBasicTimer::stop: Failed. Possibly trying to stop from a different thread"); return; } - QAbstractEventDispatcherPrivate::releaseTimerId(id); + QAbstractEventDispatcherPrivate::releaseTimerId(m_id); } - id = 0; + m_id = Qt::TimerId::Invalid; } QT_END_NAMESPACE diff --git a/src/corelib/kernel/qbasictimer.h b/src/corelib/kernel/qbasictimer.h index ccc93f6e9be..38fb24bf6fd 100644 --- a/src/corelib/kernel/qbasictimer.h +++ b/src/corelib/kernel/qbasictimer.h @@ -16,23 +16,24 @@ class QObject; class Q_CORE_EXPORT QBasicTimer { - int id; + Qt::TimerId m_id; Q_DISABLE_COPY(QBasicTimer) public: - constexpr QBasicTimer() noexcept : id{0} {} - inline ~QBasicTimer() { if (id) stop(); } + constexpr QBasicTimer() noexcept : m_id{Qt::TimerId::Invalid} {} + ~QBasicTimer() { if (isActive()) stop(); } QBasicTimer(QBasicTimer &&other) noexcept - : id{std::exchange(other.id, 0)} + : m_id{std::exchange(other.m_id, Qt::TimerId::Invalid)} {} QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QBasicTimer) - void swap(QBasicTimer &other) noexcept { std::swap(id, other.id); } + void swap(QBasicTimer &other) noexcept { std::swap(m_id, other.m_id); } - bool isActive() const noexcept { return id != 0; } - int timerId() const noexcept { return id; } + bool isActive() const noexcept { return m_id != Qt::TimerId::Invalid; } + int timerId() const noexcept { return qToUnderlying(id()); } + Qt::TimerId id() const noexcept { return m_id; } QT_CORE_INLINE_SINCE(6, 5) void start(int msec, QObject *obj); QT_CORE_INLINE_SINCE(6, 5)