summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qtimer.cpp
diff options
context:
space:
mode:
authorAhmad Samir <a.samirh78@gmail.com>2023-11-18 21:29:26 +0200
committerAhmad Samir <a.samirh78@gmail.com>2024-03-03 19:56:55 +0200
commit4bc0834bc182335984431c6a1525782efc34368c (patch)
tree87db305cc2d9e0995cf46225b3a5d6dee93c9de3 /src/corelib/kernel/qtimer.cpp
parent577a3dba521f7f69bf6129fcd28184ae288182d9 (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/qtimer.cpp')
-rw-r--r--src/corelib/kernel/qtimer.cpp35
1 files changed, 24 insertions, 11 deletions
diff --git a/src/corelib/kernel/qtimer.cpp b/src/corelib/kernel/qtimer.cpp
index e235e30a2d..5784d023ff 100644
--- a/src/corelib/kernel/qtimer.cpp
+++ b/src/corelib/kernel/qtimer.cpp
@@ -175,9 +175,21 @@ QBindable<bool> QTimer::bindableActive()
*/
int QTimer::timerId() const
{
- return d_func()->id;
+ auto v = qToUnderlying(id());
+ return v == 0 ? -1 : v;
}
+/*!
+ \since 6.8
+ Returns a Qt::TimerId representing the timer ID if the timer is running;
+ otherwise returns \c Qt::TimerId::Invalid.
+
+ \sa Qt::TimerId
+*/
+Qt::TimerId QTimer::id() const
+{
+ return d_func()->id;
+}
/*! \overload start()
@@ -193,9 +205,10 @@ void QTimer::start()
Q_D(QTimer);
if (d->isActive()) // stop running timer
stop();
- const int id = QObject::startTimer(std::chrono::milliseconds{d->inter}, d->type);
- if (id > 0) {
- d->id = id;
+
+ const auto newId = Qt::TimerId{QObject::startTimer(d->inter * 1ms, d->type)};
+ if (newId > Qt::TimerId::Invalid) {
+ d->id = newId;
d->isActiveData.notify();
}
}
@@ -249,7 +262,7 @@ void QTimer::stop()
Q_D(QTimer);
if (d->isActive()) {
QObject::killTimer(d->id);
- d->id = QTimerPrivate::INV_TIMER;
+ d->id = Qt::TimerId::Invalid;
d->isActiveData.notify();
}
}
@@ -261,7 +274,7 @@ void QTimer::stop()
void QTimer::timerEvent(QTimerEvent *e)
{
Q_D(QTimer);
- if (e->timerId() == d->id) {
+ if (Qt::TimerId{e->timerId()} == d->id) {
if (d->single)
stop();
emit timeout(QPrivateSignal());
@@ -572,14 +585,14 @@ void QTimer::setInterval(std::chrono::milliseconds interval)
d->inter.setValueBypassingBindings(msec);
if (d->isActive()) { // create new timer
QObject::killTimer(d->id); // restart timer
- const int id = QObject::startTimer(std::chrono::milliseconds{msec}, d->type);
- if (id > 0) {
+ const auto newId = Qt::TimerId{QObject::startTimer(msec * 1ms, d->type)};
+ if (newId > Qt::TimerId::Invalid) {
// Restarted successfully. No need to update the active state.
- d->id = id;
+ d->id = newId;
} else {
// Failed to start the timer.
// Need to notify about active state change.
- d->id = QTimerPrivate::INV_TIMER;
+ d->id = Qt::TimerId::Invalid;
d->isActiveData.notify();
}
}
@@ -612,7 +625,7 @@ int QTimer::remainingTime() const
{
Q_D(const QTimer);
if (d->isActive()) {
- return QAbstractEventDispatcher::instance()->remainingTime(d->id);
+ return QAbstractEventDispatcher::instance()->remainingTime(qToUnderlying(d->id));
}
return -1;