diff options
| author | Ahmad Samir <a.samirh78@gmail.com> | 2024-02-16 16:46:12 +0200 |
|---|---|---|
| committer | Ahmad Samir <a.samirh78@gmail.com> | 2024-03-02 03:31:40 +0200 |
| commit | a4a679ee796a476048ddee3f88025035cca3631a (patch) | |
| tree | 177c8500aca76b378b0bf83bc2b5100a24693e7b /src/corelib/kernel/qtimer.cpp | |
| parent | 56e151663ebfd4fc0876d33f22c81f0218339914 (diff) | |
QTimer: delegate more to the chrono overloads
singleShot() (static) methods:
QSingleShotTimer's interval isn't limited by the `int` interval in
QTimer, so this is OK, no narrowing.
start(int)/setInterval(int):
Techincally it makes no difference which overloads delegate to which,
because QTimer stores the interval in an `int` (QProperty); so any
{int interval,chrono::milliseconds.count()} > INT_MAX is narrowing
anyway. But it's less confusing and matches what has been done in other
classes when porting them to chrono, int overload delegates to chrono
overload.
Change-Id: I5ae0888f16130ae28a74be4498a180485fa34550
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/kernel/qtimer.cpp')
| -rw-r--r-- | src/corelib/kernel/qtimer.cpp | 47 |
1 files changed, 32 insertions, 15 deletions
diff --git a/src/corelib/kernel/qtimer.cpp b/src/corelib/kernel/qtimer.cpp index f93293f2832..0e09bc95657 100644 --- a/src/corelib/kernel/qtimer.cpp +++ b/src/corelib/kernel/qtimer.cpp @@ -219,7 +219,15 @@ void QTimer::start() */ void QTimer::start(int msec) { + start(msec * 1ms); +} + +void QTimer::start(std::chrono::milliseconds interval) +{ Q_D(QTimer); + // This could be narrowing as the interval is stored in an `int` QProperty, + // and the type can't be changed in Qt6. + const int msec = interval.count(); const bool intervalChanged = msec != d->inter; d->inter.setValue(msec); start(); @@ -268,14 +276,13 @@ void QTimer::timerEvent(QTimerEvent *e) \a timerType is the timer type \a receiver is the receiver object, can be null. In such a case, it will be the same as the final sender class. - \a slot a pointer only used when using Qt::UniqueConnection \a slotObj the slot object - */ -void QTimer::singleShotImpl(int msec, Qt::TimerType timerType, +*/ +void QTimer::singleShotImpl(std::chrono::milliseconds msec, Qt::TimerType timerType, const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj) { - if (msec == 0) { + if (msec == 0ms) { bool deleteReceiver = false; // Optimize: set a receiver context when none is given, such that we can use // QMetaObject::invokeMethod which is more efficient than going through a timer. @@ -304,11 +311,13 @@ void QTimer::singleShotImpl(int msec, Qt::TimerType timerType, return; } - new QSingleShotTimer(msec * 1ms, timerType, receiver, slotObj); + new QSingleShotTimer(msec, timerType, receiver, slotObj); } /*! + \fn void QTimer::singleShot(int msec, const QObject *receiver, const char *member) \reentrant + \deprecated [6.8] Use the chrono overloads. This static function calls a slot after a given time interval. It is very convenient to use this function because you do not need @@ -327,13 +336,11 @@ void QTimer::singleShotImpl(int msec, Qt::TimerType timerType, \sa start() */ -void QTimer::singleShot(int msec, const QObject *receiver, const char *member) -{ - singleShot(msec, defaultTypeFor(msec), receiver, member); -} - -/*! \overload +/*! + \fn void QTimer::singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, const char *member) + \overload \reentrant + \deprecated [6.8] Use the chrono overloads. This static function calls a slot after a given time interval. It is very convenient to use this function because you do not need @@ -346,14 +353,16 @@ void QTimer::singleShot(int msec, const QObject *receiver, const char *member) \sa start() */ -void QTimer::singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, const char *member) + +void QTimer::singleShot(std::chrono::milliseconds msec, Qt::TimerType timerType, + const QObject *receiver, const char *member) { - if (Q_UNLIKELY(msec < 0)) { + if (Q_UNLIKELY(msec < 0ms)) { qWarning("QTimer::singleShot: Timers cannot have negative timeouts"); return; } if (receiver && member) { - if (msec == 0) { + if (msec == 0ms) { // special code shortpath for 0-timers const char* bracketPosition = strchr(member, '('); if (!bracketPosition || !(member[0] >= '0' && member[0] <= '2')) { @@ -366,7 +375,7 @@ void QTimer::singleShot(int msec, Qt::TimerType timerType, const QObject *receiv Qt::QueuedConnection); return; } - (void) new QSingleShotTimer(msec * 1ms, timerType, receiver, member); + (void) new QSingleShotTimer(msec, timerType, receiver, member); } } @@ -548,7 +557,15 @@ QBindable<bool> QTimer::bindableSingleShot() */ void QTimer::setInterval(int msec) { + setInterval(std::chrono::milliseconds{msec}); +} + +void QTimer::setInterval(std::chrono::milliseconds interval) +{ Q_D(QTimer); + // This could be narrowing as the interval is stored in an `int` QProperty, + // and the type can't be changed in Qt6. + const int msec = interval.count(); d->inter.removeBindingUnlessInWrapper(); const bool intervalChanged = msec != d->inter.valueBypassingBindings(); d->inter.setValueBypassingBindings(msec); |
