Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qchronotimer.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QCHRONOTIMER_H
5#define QCHRONOTIMER_H
6
7#ifndef QT_NO_QOBJECT
8
9#include <QtCore/qcoreevent.h>
10#include <QtCore/qnamespace.h>
11#include <QtCore/qobject.h>
12#include <QtCore/qproperty.h>
13
14#include <chrono>
15
17
18class QTimerPrivate;
19class Q_CORE_EXPORT QChronoTimer : public QObject
20{
22 Q_PROPERTY(bool singleShot READ isSingleShot WRITE setSingleShot
23 BINDABLE bindableSingleShot FINAL)
24 Q_PROPERTY(std::chrono::nanoseconds interval READ interval WRITE setInterval
25 BINDABLE bindableInterval FINAL)
26 Q_PROPERTY(std::chrono::nanoseconds remainingTime READ remainingTime FINAL)
27 Q_PROPERTY(Qt::TimerType timerType READ timerType WRITE setTimerType
28 BINDABLE bindableTimerType FINAL)
29 Q_PROPERTY(bool active READ isActive STORED false BINDABLE bindableActive FINAL)
30
31 template <typename Functor>
32 using FunctorContext = typename QtPrivate::ContextTypeForFunctor<Functor>::ContextType;
33
34public:
35 explicit QChronoTimer(std::chrono::nanoseconds nsec, QObject *parent = nullptr);
36 explicit QChronoTimer(QObject *parent = nullptr);
37 ~QChronoTimer() override;
38
39 bool isActive() const;
40 QBindable<bool> bindableActive();
41 Qt::TimerId id() const;
42
43 void setInterval(std::chrono::nanoseconds nsec);
44 std::chrono::nanoseconds interval() const;
45 QBindable<std::chrono::nanoseconds> bindableInterval();
46
47 std::chrono::nanoseconds remainingTime() const;
48
49 void setTimerType(Qt::TimerType atype);
50 Qt::TimerType timerType() const;
51 QBindable<Qt::TimerType> bindableTimerType();
52
53 void setSingleShot(bool singleShot);
54 bool isSingleShot() const;
55 QBindable<bool> bindableSingleShot();
56
57 // singleShot with context
58#ifdef Q_QDOC
59 template <typename Functor>
60 static inline void singleShot(std::chrono::nanoseconds interval,
61 const QObject *receiver, Functor &&slot);
62 template <typename Functor>
63 static inline void singleShot(std::chrono::nanoseconds interval interval,
64 Qt::TimerType timerType,
65 const QObject *receiver, Functor &&slot);
66#else
67 template <typename Functor>
68 static void singleShot(std::chrono::nanoseconds interval,
69 const FunctorContext<Functor> *receiver, Functor &&slot)
70 {
71 singleShot(interval, defaultTimerTypeFor(interval), receiver, std::forward<Functor>(slot));
72 }
73 template <typename Functor>
74 static void singleShot(std::chrono::nanoseconds interval, Qt::TimerType timerType,
75 const FunctorContext<Functor> *receiver, Functor &&slot)
76 {
77 using Prototype = void(*)();
78 auto *slotObj = QtPrivate::makeCallableObject<Prototype>(std::forward<Functor>(slot));
79 singleShotImpl(interval, timerType, receiver, slotObj);
80 }
81#endif
82
83 template <typename Functor>
84 static void singleShot(std::chrono::nanoseconds interval, Qt::TimerType timerType,
85 Functor &&slot)
86 { singleShot(interval, timerType, nullptr, std::forward<Functor>(slot)); }
87
88 template <typename Functor>
89 static void singleShot(std::chrono::nanoseconds interval, Functor &&slot)
90 {
91 singleShot(interval, defaultTimerTypeFor(interval), nullptr, std::forward<Functor>(slot));
92 }
93
94 static void singleShot(std::chrono::nanoseconds interval, Qt::TimerType timerType,
95 const QObject *receiver, const char *member);
96 static void singleShot(std::chrono::nanoseconds interval, const QObject *receiver,
97 const char *member)
98 { singleShot(interval, defaultTimerTypeFor(interval), receiver, member); }
99
100#ifdef Q_QDOC
101 template <typename Functor>
102 QMetaObject::Connection callOnTimeout(const QObject *context, Functor &&slot,
103 Qt::ConnectionType connectionType = Qt::AutoConnection);
104#else
105 template <typename ... Args>
107 {
108 return QObject::connect(this, &QChronoTimer::timeout, std::forward<Args>(args)... );
109 }
110#endif
111
112public Q_SLOTS:
113 void start();
114 void stop();
115
117 void timeout(QPrivateSignal);
118
119protected:
120 void timerEvent(QTimerEvent *) override;
121
122private:
123 Q_DISABLE_COPY(QChronoTimer)
124
125 // QChronoTimer uses QTimerPrivate
126 inline QTimerPrivate *d_func() noexcept
127 { Q_CAST_IGNORE_ALIGN(return reinterpret_cast<QTimerPrivate *>(qGetPtrHelper(d_ptr));) }
128 inline const QTimerPrivate *d_func() const noexcept
129 { Q_CAST_IGNORE_ALIGN(return reinterpret_cast<const QTimerPrivate *>(qGetPtrHelper(d_ptr));) }
130
131 // These two functions are inherited from QObject
132 int startTimer(std::chrono::nanoseconds) = delete;
133 void killTimer(int) = delete;
134
135 static constexpr Qt::TimerType defaultTimerTypeFor(std::chrono::nanoseconds interval) noexcept
136 {
137 using namespace std::chrono_literals;
138 return interval >= 2s ? Qt::CoarseTimer : Qt::PreciseTimer;
139 }
140
141 static void singleShotImpl(std::chrono::nanoseconds interval, Qt::TimerType timerType,
142 const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj);
143};
144
146
147#endif // QT_NO_QOBJECT
148
149#endif // QCHRONOTIMER_H
bool isActive
\inmodule QtCore
Definition qproperty.h:811
\inmodule QtCore
static void singleShot(std::chrono::nanoseconds interval, Qt::TimerType timerType, const FunctorContext< Functor > *receiver, Functor &&slot)
static void singleShot(std::chrono::nanoseconds interval, const QObject *receiver, const char *member)
static void singleShot(std::chrono::nanoseconds interval, Functor &&slot)
static void singleShot(std::chrono::nanoseconds interval, Qt::TimerType timerType, Functor &&slot)
static void singleShot(std::chrono::nanoseconds interval, const FunctorContext< Functor > *receiver, Functor &&slot)
void timeout(QPrivateSignal)
This signal is emitted when the timer times out.
QMetaObject::Connection callOnTimeout(Args &&...args)
\inmodule QtCore Represents a handle to a signal-slot (or signal-functor) connection.
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
\inmodule QtCore
Definition qcoreevent.h:366
Combined button and popup list for selecting options.
\macro QT_NO_KEYWORDS >
Definition qcompare.h:63
TimerType
@ CoarseTimer
@ PreciseTimer
ConnectionType
@ AutoConnection
static void * context
#define Q_CAST_IGNORE_ALIGN(body)
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
GLuint start
GLdouble s
[6]
Definition qopenglext.h:235
#define Q_PROPERTY(...)
#define Q_OBJECT
#define Q_SLOTS
#define Q_SIGNALS
#define explicit
QJSValueList args