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>
bb10
Ahmad Samir 2024-02-16 16:46:12 +02:00
parent 56e151663e
commit a4a679ee79
3 changed files with 59 additions and 34 deletions

View File

@ -919,6 +919,8 @@ QUrl QUrl::fromEncoded(const QByteArray &input, ParsingMode mode)
return QUrl::fromEncoded(QByteArrayView(input), mode);
}
#include "qtimer.h" // inlined API
// #include "qotherheader.h"
// // implement removed functions from qotherheader.h

View File

@ -218,8 +218,16 @@ void QTimer::start()
cause trouble and highly erratic behavior of the UI.
*/
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);
}
}
@ -547,8 +556,16 @@ QBindable<bool> QTimer::bindableSingleShot()
\sa singleShot
*/
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);

View File

@ -46,7 +46,10 @@ public:
bool isSingleShot() const;
QBindable<bool> bindableSingleShot();
QT_CORE_INLINE_SINCE(6, 8)
static void singleShot(int msec, const QObject *receiver, const char *member);
QT_CORE_INLINE_SINCE(6, 8)
static void singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, const char *member);
// singleShot with context
@ -111,10 +114,7 @@ Q_SIGNALS:
void timeout(QPrivateSignal);
public:
void setInterval(std::chrono::milliseconds value)
{
setInterval(int(value.count()));
}
void setInterval(std::chrono::milliseconds value);
std::chrono::milliseconds intervalAsDuration() const
{
@ -128,18 +128,12 @@ public:
static void singleShot(std::chrono::milliseconds value, const QObject *receiver, const char *member)
{
singleShot(int(value.count()), receiver, member);
singleShot(value, defaultTypeFor(value), receiver, member);
}
static void singleShot(std::chrono::milliseconds interval, Qt::TimerType timerType,
const QObject *receiver, const char *member);
static void singleShot(std::chrono::milliseconds value, Qt::TimerType timerType, const QObject *receiver, const char *member)
{
singleShot(int(value.count()), timerType, receiver, member);
}
void start(std::chrono::milliseconds value)
{
start(int(value.count()));
}
void start(std::chrono::milliseconds value);
protected:
void timerEvent(QTimerEvent *) override;
@ -163,17 +157,29 @@ private:
return interval >= 2s ? Qt::CoarseTimer : Qt::PreciseTimer;
}
QT_CORE_INLINE_SINCE(6, 8)
static void singleShotImpl(int msec, Qt::TimerType timerType,
const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj);
static void singleShotImpl(std::chrono::milliseconds interval, Qt::TimerType timerType,
const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj)
{
singleShotImpl(int(interval.count()),
timerType, receiver, slotObj);
}
const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj);
};
#if QT_CORE_INLINE_IMPL_SINCE(6, 8)
void QTimer::singleShot(int msec, const QObject *receiver, const char *member)
{ singleShot(std::chrono::milliseconds{msec}, receiver, member); }
void QTimer::singleShot(int msec, Qt::TimerType timerType, const QObject *receiver,
const char *member)
{ singleShot(std::chrono::milliseconds{msec}, timerType, receiver, member); }
void QTimer::singleShotImpl(int msec, Qt::TimerType timerType,
const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj)
{
singleShotImpl(std::chrono::milliseconds{msec}, timerType, receiver, slotObj);
}
#endif
QT_END_NAMESPACE
#endif // QT_NO_QOBJECT