QBasicTimer: port to Qt::TimerId

[ChangeLog][QtCore][QBasicTimer] Added id() method returning
Qt::TimerId.

Fixes: QTBUG-128144
Change-Id: Idbe29311cca3befb771dbf1ef976a42e65e59ce9
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 751cbbd6b13d9899e31c19d9db80d1c64a72a8bd)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Ahmad Samir 2024-08-18 23:32:35 +03:00 committed by Qt Cherry-pick Bot
parent 0945ca9a01
commit c9fe636dfc
2 changed files with 25 additions and 12 deletions

View File

@ -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

View File

@ -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)