Qt 6.x
The Qt SDK
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
35 void setInterval(int msec);
36 int interval() const;
37 QBindable<int> bindableInterval();
38
39 int remainingTime() const;
40
41 void setTimerType(Qt::TimerType atype);
42 Qt::TimerType timerType() const;
43 QBindable<Qt::TimerType> bindableTimerType();
44
45 void setSingleShot(bool singleShot);
46 bool isSingleShot() const;
47 QBindable<bool> bindableSingleShot();
48
49 static void singleShot(int msec, const QObject *receiver, const char *member);
50 static void singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, const char *member);
51
52 // singleShot with context
53 template <typename Duration, typename Functor>
54 static inline void singleShot(Duration interval,
55#ifdef Q_QDOC
56 const QObject *receiver,
57#else
59#endif
60
61 Functor &&slot)
62 {
63 singleShot(interval, defaultTypeFor(interval), receiver, std::forward<Functor>(slot));
64 }
65 template <typename Duration, typename Functor>
66 static inline void singleShot(Duration interval, Qt::TimerType timerType,
67#ifdef Q_QDOC
68 const QObject *receiver,
69#else
71#endif
72 Functor &&slot)
73 {
74 using Prototype = void(*)();
75 singleShotImpl(interval, timerType, receiver,
76 QtPrivate::makeCallableObject<Prototype>(std::forward<Functor>(slot)));
77 }
78 // singleShot without context
79 template <typename Duration, typename Functor>
80 static inline void singleShot(Duration interval, Functor &&slot)
81 {
82 singleShot(interval, defaultTypeFor(interval), nullptr, std::forward<Functor>(slot));
83 }
84 template <typename Duration, typename Functor>
85 static inline void singleShot(Duration interval, Qt::TimerType timerType, Functor &&slot)
86 {
87 singleShot(interval, timerType, nullptr, std::forward<Functor>(slot));
88 }
89
90#ifdef Q_QDOC
91 template <typename Functor>
92 QMetaObject::Connection callOnTimeout(Functor &&slot, Qt::ConnectionType connectionType = Qt::AutoConnection);
93 template <typename Functor>
94 QMetaObject::Connection callOnTimeout(const QObject *context, Functor &&slot, Qt::ConnectionType connectionType = Qt::AutoConnection);
95#else
96 template <typename ... Args>
98 {
99 return QObject::connect(this, &QTimer::timeout, std::forward<Args>(args)... );
100 }
101
102#endif
103
104public Q_SLOTS:
105 void start(int msec);
106
107 void start();
108 void stop();
109
111 void timeout(QPrivateSignal);
112
113public:
114 void setInterval(std::chrono::milliseconds value)
115 {
116 setInterval(int(value.count()));
117 }
118
119 std::chrono::milliseconds intervalAsDuration() const
120 {
121 return std::chrono::milliseconds(interval());
122 }
123
124 std::chrono::milliseconds remainingTimeAsDuration() const
125 {
126 return std::chrono::milliseconds(remainingTime());
127 }
128
129 static void singleShot(std::chrono::milliseconds value, const QObject *receiver, const char *member)
130 {
131 singleShot(int(value.count()), receiver, member);
132 }
133
134 static void singleShot(std::chrono::milliseconds value, Qt::TimerType timerType, const QObject *receiver, const char *member)
135 {
136 singleShot(int(value.count()), timerType, receiver, member);
137 }
138
139 void start(std::chrono::milliseconds value)
140 {
141 start(int(value.count()));
142 }
143
144protected:
145 void timerEvent(QTimerEvent *) override;
146
147private:
148 Q_DISABLE_COPY(QTimer)
149 Q_DECLARE_PRIVATE(QTimer)
150
151 inline int startTimer(int){ return -1;}
152 inline void killTimer(int){}
153
154 static constexpr Qt::TimerType defaultTypeFor(int msecs) noexcept
155 { return msecs >= 2000 ? Qt::CoarseTimer : Qt::PreciseTimer; }
156 static void singleShotImpl(int msec, Qt::TimerType timerType,
157 const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj);
158
159 static Qt::TimerType defaultTypeFor(std::chrono::milliseconds interval)
160 { return defaultTypeFor(int(interval.count())); }
161
162 static void singleShotImpl(std::chrono::milliseconds interval, Qt::TimerType timerType,
163 const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj)
164 {
165 singleShotImpl(int(interval.count()),
166 timerType, receiver, slotObj);
167 }
168};
169
171
172#endif // QT_NO_QOBJECT
173
174#endif // QTIMER_H
bool isActive
\inmodule QtCore
Definition qproperty.h:809
\inmodule QtCore Represents a handle to a signal-slot (or signal-functor) connection.
\inmodule QtCore
Definition qobject.h:90
int startTimer(int interval, Qt::TimerType timerType=Qt::CoarseTimer)
This is an overloaded function that will start a timer of type timerType and a timeout of interval mi...
Definition qobject.cpp:1792
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2823
virtual void timerEvent(QTimerEvent *event)
This event handler can be reimplemented in a subclass to receive timer events for the object.
Definition qobject.cpp:1433
void killTimer(int id)
Kills the timer with timer identifier, id.
Definition qobject.cpp:1872
\inmodule QtCore
Definition qcoreevent.h:359
\inmodule QtCore
Definition qtimer.h:20
static void singleShot(Duration interval, Functor &&slot)
Definition qtimer.h:80
void start(std::chrono::milliseconds value)
Definition qtimer.h:139
std::chrono::milliseconds intervalAsDuration() const
Definition qtimer.h:119
static void singleShot(Duration interval, Qt::TimerType timerType, const typename QtPrivate::ContextTypeForFunctor< Functor >::ContextType *receiver, Functor &&slot)
Definition qtimer.h:66
void setInterval(std::chrono::milliseconds value)
Definition qtimer.h:114
static void singleShot(Duration interval, Qt::TimerType timerType, Functor &&slot)
Definition qtimer.h:85
static void singleShot(Duration interval, const typename QtPrivate::ContextTypeForFunctor< Functor >::ContextType *receiver, Functor &&slot)
Definition qtimer.h:54
static void singleShot(std::chrono::milliseconds value, const QObject *receiver, const char *member)
Definition qtimer.h:129
void timeout(QPrivateSignal)
This signal is emitted when the timer times out.
QMetaObject::Connection callOnTimeout(Args &&...args)
Definition qtimer.h:97
std::chrono::milliseconds remainingTimeAsDuration() const
Definition qtimer.h:124
static void singleShot(std::chrono::milliseconds value, Qt::TimerType timerType, const QObject *receiver, const char *member)
Definition qtimer.h:134
Combined button and popup list for selecting options.
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
#define Q_PROPERTY(...)
#define Q_OBJECT
#define Q_SLOTS
#define Q_SIGNALS
QJSValueList args
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent