QObject: add startTimer() overload with std::chrono
In client code I often see code like: startTimer(1000); //ms Let the code to be self-explaining. So provide overload method that takes std::chrono::milliseconds as arg. QTimer already has std::chrono support, but QObject does not. Change-Id: Ib348612ce35f1a997b4816fe9e864775cbcbec16 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
parent
3e7f42cb81
commit
6d1d66a042
@ -156,6 +156,17 @@ MyObject::MyObject(QObject *parent)
|
||||
startTimer(50); // 50-millisecond timer
|
||||
startTimer(1000); // 1-second timer
|
||||
startTimer(60000); // 1-minute timer
|
||||
|
||||
using namespace std::chrono;
|
||||
startTimer(milliseconds(50));
|
||||
startTimer(seconds(1));
|
||||
startTimer(minutes(1));
|
||||
|
||||
// since C++14 we can use std::chrono::duration literals, e.g.:
|
||||
startTimer(100ms);
|
||||
startTimer(5s);
|
||||
startTimer(2min);
|
||||
startTimer(1h);
|
||||
}
|
||||
|
||||
void MyObject::timerEvent(QTimerEvent *event)
|
||||
|
@ -1633,6 +1633,45 @@ int QObject::startTimer(int interval, Qt::TimerType timerType)
|
||||
return timerId;
|
||||
}
|
||||
|
||||
/*!
|
||||
\since 5.9
|
||||
\overload
|
||||
\fn int QObject::startTimer(std::chrono::milliseconds time, Qt::TimerType timerType)
|
||||
|
||||
Starts a timer and returns a timer identifier, or returns zero if
|
||||
it could not start a timer.
|
||||
|
||||
A timer event will occur every \a time interval until killTimer()
|
||||
is called. If \a interval is equal to \c{std::chrono::duration::zero()},
|
||||
then the timer event occurs once every time there are no more window
|
||||
system events to process.
|
||||
|
||||
The virtual timerEvent() function is called with the QTimerEvent
|
||||
event parameter class when a timer event occurs. Reimplement this
|
||||
function to get timer events.
|
||||
|
||||
If multiple timers are running, the QTimerEvent::timerId() can be
|
||||
used to find out which timer was activated.
|
||||
|
||||
Example:
|
||||
|
||||
\snippet code/src_corelib_kernel_qobject.cpp 8
|
||||
|
||||
Note that QTimer's accuracy depends on the underlying operating system and
|
||||
hardware. The \a timerType argument allows you to customize the accuracy of
|
||||
the timer. See Qt::TimerType for information on the different timer types.
|
||||
Most platforms support an accuracy of 20 milliseconds; some provide more.
|
||||
If Qt is unable to deliver the requested number of timer events, it will
|
||||
silently discard some.
|
||||
|
||||
The QTimer class provides a high-level programming interface with
|
||||
single-shot timers and timer signals instead of events. There is
|
||||
also a QBasicTimer class that is more lightweight than QTimer and
|
||||
less clumsy than using timer IDs directly.
|
||||
|
||||
\sa timerEvent(), killTimer(), QTimer::singleShot()
|
||||
*/
|
||||
|
||||
/*!
|
||||
Kills the timer with timer identifier, \a id.
|
||||
|
||||
|
@ -55,6 +55,10 @@
|
||||
|
||||
#include <QtCore/qobject_impl.h>
|
||||
|
||||
#if QT_HAS_INCLUDE(<chrono>)
|
||||
# include <chrono>
|
||||
#endif
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
|
||||
@ -150,6 +154,13 @@ public:
|
||||
void moveToThread(QThread *thread);
|
||||
|
||||
int startTimer(int interval, Qt::TimerType timerType = Qt::CoarseTimer);
|
||||
#if QT_HAS_INCLUDE(<chrono>) || defined(Q_QDOC)
|
||||
Q_ALWAYS_INLINE
|
||||
int startTimer(std::chrono::milliseconds time, Qt::TimerType timerType = Qt::CoarseTimer)
|
||||
{
|
||||
return startTimer(int(time.count()), timerType);
|
||||
}
|
||||
#endif
|
||||
void killTimer(int id);
|
||||
|
||||
template<typename T>
|
||||
|
Loading…
x
Reference in New Issue
Block a user