diff options
author | Anton Kudryavtsev <antkudr@mail.ru> | 2016-11-28 21:29:22 +0300 |
---|---|---|
committer | Anton Kudryavtsev <antkudr@mail.ru> | 2016-12-23 18:08:53 +0000 |
commit | 6d1d66a0429d2eb36beb192112b9a7bb8ae00b9a (patch) | |
tree | 2176395ae84038d4a298f668dd58780da0620671 | |
parent | 3e7f42cb81cacd7e07c20a69ae3e86e5316e2b99 (diff) |
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>
-rw-r--r-- | src/corelib/doc/snippets/code/src_corelib_kernel_qobject.cpp | 11 | ||||
-rw-r--r-- | src/corelib/kernel/qobject.cpp | 39 | ||||
-rw-r--r-- | src/corelib/kernel/qobject.h | 11 |
3 files changed, 61 insertions, 0 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_kernel_qobject.cpp b/src/corelib/doc/snippets/code/src_corelib_kernel_qobject.cpp index f852988e9a..c12ed147db 100644 --- a/src/corelib/doc/snippets/code/src_corelib_kernel_qobject.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_kernel_qobject.cpp @@ -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) diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index fd1c767b4c..ac1b2d41bf 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -1634,6 +1634,45 @@ int QObject::startTimer(int interval, Qt::TimerType timerType) } /*! + \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. The timer identifier is returned by startTimer() when a timer diff --git a/src/corelib/kernel/qobject.h b/src/corelib/kernel/qobject.h index 25acdefeaf..d42eb2a13d 100644 --- a/src/corelib/kernel/qobject.h +++ b/src/corelib/kernel/qobject.h @@ -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> |