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
qtimer.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 QTIMER_H
5#define QTIMER_H
6
7#include <QtCore/qglobal.h>
8
9#ifndef QT_NO_QOBJECT
10
11#include <QtCore/qbasictimer.h> // conceptual inheritance
12#include <QtCore/qobject.h>
13
14#include <chrono>
15
17
18class QTimerPrivate;
19class Q_CORE_EXPORT QTimer : public QObject
20{
22 Q_PROPERTY(bool singleShot READ isSingleShot WRITE setSingleShot BINDABLE bindableSingleShot)
23 Q_PROPERTY(int interval READ interval WRITE setInterval BINDABLE bindableInterval)
24 Q_PROPERTY(int remainingTime READ remainingTime)
25 Q_PROPERTY(Qt::TimerType timerType READ timerType WRITE setTimerType BINDABLE bindableTimerType)
26 Q_PROPERTY(bool active READ isActive STORED false BINDABLE bindableActive)
27public:
28 explicit QTimer(QObject *parent = nullptr);
29 ~QTimer();
30
31 bool isActive() const;
32 QBindable<bool> bindableActive();
33 int timerId() const;
34 Qt::TimerId id() const;
35
36 void setInterval(int msec);
37 int interval() const;
38 QBindable<int> bindableInterval();
39
40 int remainingTime() const;
41
42 void setTimerType(Qt::TimerType atype);
43 Qt::TimerType timerType() const;
44 QBindable<Qt::TimerType> bindableTimerType();
45
46 void setSingleShot(bool singleShot);
47 bool isSingleShot() const;
48 QBindable<bool> bindableSingleShot();
49
50 QT_CORE_INLINE_SINCE(6, 8)
51 static void singleShot(int msec, const QObject *receiver, const char *member);
52
53 QT_CORE_INLINE_SINCE(6, 8)
54 static void singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, const char *member);
55
56 // singleShot with context
57#ifdef Q_QDOC
58 template <typename Duration, typename Functor>
59 static inline void singleShot(Duration interval, const QObject *receiver, Functor &&slot);
60 template <typename Duration, typename Functor>
61 static inline void singleShot(Duration interval, Qt::TimerType timerType,
62 const QObject *receiver, Functor &&slot);
63#else
64 template <typename Duration, typename Functor>
65 static inline void singleShot(Duration interval,
67 Functor &&slot)
68 {
69 singleShot(interval, defaultTypeFor(interval), receiver, std::forward<Functor>(slot));
70 }
71 template <typename Duration, typename Functor>
72 static inline void singleShot(Duration interval, Qt::TimerType timerType,
74 Functor &&slot)
75 {
76 using Prototype = void(*)();
77 singleShotImpl(interval, timerType, receiver,
78 QtPrivate::makeCallableObject<Prototype>(std::forward<Functor>(slot)));
79 }
80#endif
81
82 // singleShot without context
83 template <typename Duration, typename Functor>
84 static inline void singleShot(Duration interval, Functor &&slot)
85 {
86 singleShot(interval, defaultTypeFor(interval), nullptr, std::forward<Functor>(slot));
87 }
88 template <typename Duration, typename Functor>
89 static inline void singleShot(Duration interval, Qt::TimerType timerType, Functor &&slot)
90 {
91 singleShot(interval, timerType, nullptr, std::forward<Functor>(slot));
92 }
93
94#ifdef Q_QDOC
95 template <typename Functor>
96 QMetaObject::Connection callOnTimeout(Functor &&slot);
97 template <typename Functor>
98 QMetaObject::Connection callOnTimeout(const QObject *context, Functor &&slot, Qt::ConnectionType connectionType = Qt::AutoConnection);
99#else
100 template <typename ... Args>
102 {
103 return QObject::connect(this, &QTimer::timeout, std::forward<Args>(args)... );
104 }
105
106#endif
107
108public Q_SLOTS:
109 void start(int msec);
110
111 void start();
112 void stop();
113
115 void timeout(QPrivateSignal);
116
117public:
118 void setInterval(std::chrono::milliseconds value);
119
120 std::chrono::milliseconds intervalAsDuration() const
121 {
122 return std::chrono::milliseconds(interval());
123 }
124
125 std::chrono::milliseconds remainingTimeAsDuration() const
126 {
127 return std::chrono::milliseconds(remainingTime());
128 }
129
130 static void singleShot(std::chrono::milliseconds value, const QObject *receiver, const char *member)
131 {
132 singleShot(value, defaultTypeFor(value), receiver, member);
133 }
134 static void singleShot(std::chrono::milliseconds interval, Qt::TimerType timerType,
135 const QObject *receiver, const char *member);
136
137 void start(std::chrono::milliseconds value);
138
139protected:
140 void timerEvent(QTimerEvent *) override;
141
142private:
143 Q_DISABLE_COPY(QTimer)
144 Q_DECLARE_PRIVATE(QTimer)
145
146 inline int startTimer(int){ return -1;}
147 inline void killTimer(int){}
148
149 static constexpr Qt::TimerType defaultTypeFor(int msecs) noexcept
150 { return defaultTypeFor(std::chrono::milliseconds{msecs}); }
151
152 static constexpr Qt::TimerType defaultTypeFor(std::chrono::milliseconds interval) noexcept
153 {
154 // coarse timers are worst in their first firing
155 // so we prefer a high precision timer for something that happens only once
156 // unless the timeout is too big, in which case we go for coarse anyway
157 using namespace std::chrono_literals;
158 return interval >= 2s ? Qt::CoarseTimer : Qt::PreciseTimer;
159 }
160
161 QT_CORE_INLINE_SINCE(6, 8)
162 static void singleShotImpl(int msec, Qt::TimerType timerType,
163 const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj);
164
165 static void singleShotImpl(std::chrono::milliseconds interval, Qt::TimerType timerType,
166 const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj);
167};
168
169#if QT_CORE_INLINE_IMPL_SINCE(6, 8)
170void QTimer::singleShot(int msec, const QObject *receiver, const char *member)
171{ singleShot(std::chrono::milliseconds{msec}, receiver, member); }
172
173void QTimer::singleShot(int msec, Qt::TimerType timerType, const QObject *receiver,
174 const char *member)
175{ singleShot(std::chrono::milliseconds{msec}, timerType, receiver, member); }
176
177void QTimer::singleShotImpl(int msec, Qt::TimerType timerType,
178 const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj)
179{
180 singleShotImpl(std::chrono::milliseconds{msec}, timerType, receiver, slotObj);
181}
182#endif
183
185
186#endif // QT_NO_QOBJECT
187
188#endif // QTIMER_H
bool isActive
\inmodule QtCore
Definition qproperty.h:811
\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
\inmodule QtCore
Definition qtimer.h:20
static void singleShot(Duration interval, Functor &&slot)
Definition qtimer.h:84
std::chrono::milliseconds intervalAsDuration() const
Definition qtimer.h:120
static void singleShot(Duration interval, Qt::TimerType timerType, const typename QtPrivate::ContextTypeForFunctor< Functor >::ContextType *receiver, Functor &&slot)
Definition qtimer.h:72
bool singleShot
whether the timer is a single-shot timer
Definition qtimer.h:22
static void singleShot(Duration interval, Qt::TimerType timerType, Functor &&slot)
Definition qtimer.h:89
static void singleShot(Duration interval, const typename QtPrivate::ContextTypeForFunctor< Functor >::ContextType *receiver, Functor &&slot)
Definition qtimer.h:65
static void singleShot(std::chrono::milliseconds value, const QObject *receiver, const char *member)
Definition qtimer.h:130
void timeout(QPrivateSignal)
This signal is emitted when the timer times out.
QMetaObject::Connection callOnTimeout(Args &&...args)
Definition qtimer.h:101
std::chrono::milliseconds remainingTimeAsDuration() const
Definition qtimer.h:125
Combined button and popup list for selecting options.
\macro QT_NO_KEYWORDS >
Definition qcompare.h:63
TimerType
@ CoarseTimer
@ PreciseTimer
ConnectionType
@ AutoConnection
static void * context
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
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
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