File indexing completed on 2025-09-17 09:09:36
0001
0002
0003
0004 #ifndef QTIMER_H
0005 #define QTIMER_H
0006
0007 #include <QtCore/qglobal.h>
0008
0009 #ifndef QT_NO_QOBJECT
0010
0011 #include <QtCore/qbasictimer.h> // conceptual inheritance
0012 #include <QtCore/qobject.h>
0013
0014 #include <chrono>
0015
0016 QT_BEGIN_NAMESPACE
0017
0018 class QTimerPrivate;
0019 class Q_CORE_EXPORT QTimer : public QObject
0020 {
0021 Q_OBJECT
0022 Q_PROPERTY(bool singleShot READ isSingleShot WRITE setSingleShot BINDABLE bindableSingleShot)
0023 Q_PROPERTY(int interval READ interval WRITE setInterval BINDABLE bindableInterval)
0024 Q_PROPERTY(int remainingTime READ remainingTime)
0025 Q_PROPERTY(Qt::TimerType timerType READ timerType WRITE setTimerType BINDABLE bindableTimerType)
0026 Q_PROPERTY(bool active READ isActive STORED false BINDABLE bindableActive)
0027 public:
0028 explicit QTimer(QObject *parent = nullptr);
0029 ~QTimer();
0030
0031 bool isActive() const;
0032 QBindable<bool> bindableActive();
0033 int timerId() const;
0034 Qt::TimerId id() const;
0035
0036 void setInterval(int msec);
0037 int interval() const;
0038 QBindable<int> bindableInterval();
0039
0040 int remainingTime() const;
0041
0042 void setTimerType(Qt::TimerType atype);
0043 Qt::TimerType timerType() const;
0044 QBindable<Qt::TimerType> bindableTimerType();
0045
0046 void setSingleShot(bool singleShot);
0047 bool isSingleShot() const;
0048 QBindable<bool> bindableSingleShot();
0049
0050 QT_CORE_INLINE_SINCE(6, 8)
0051 static void singleShot(int msec, const QObject *receiver, const char *member);
0052
0053 QT_CORE_INLINE_SINCE(6, 8)
0054 static void singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, const char *member);
0055
0056
0057 #ifdef Q_QDOC
0058 template <typename Duration, typename Functor>
0059 static inline void singleShot(Duration interval, const QObject *receiver, Functor &&slot);
0060 template <typename Duration, typename Functor>
0061 static inline void singleShot(Duration interval, Qt::TimerType timerType,
0062 const QObject *receiver, Functor &&slot);
0063 #else
0064 template <typename Duration, typename Functor>
0065 static inline void singleShot(Duration interval,
0066 const typename QtPrivate::ContextTypeForFunctor<Functor>::ContextType *receiver,
0067 Functor &&slot)
0068 {
0069 singleShot(interval, defaultTypeFor(interval), receiver, std::forward<Functor>(slot));
0070 }
0071 template <typename Duration, typename Functor>
0072 static inline void singleShot(Duration interval, Qt::TimerType timerType,
0073 const typename QtPrivate::ContextTypeForFunctor<Functor>::ContextType *receiver,
0074 Functor &&slot)
0075 {
0076 using Prototype = void(*)();
0077 singleShotImpl(interval, timerType, receiver,
0078 QtPrivate::makeCallableObject<Prototype>(std::forward<Functor>(slot)));
0079 }
0080 #endif
0081
0082
0083 template <typename Duration, typename Functor>
0084 static inline void singleShot(Duration interval, Functor &&slot)
0085 {
0086 singleShot(interval, defaultTypeFor(interval), nullptr, std::forward<Functor>(slot));
0087 }
0088 template <typename Duration, typename Functor>
0089 static inline void singleShot(Duration interval, Qt::TimerType timerType, Functor &&slot)
0090 {
0091 singleShot(interval, timerType, nullptr, std::forward<Functor>(slot));
0092 }
0093
0094 #ifdef Q_QDOC
0095 template <typename Functor>
0096 QMetaObject::Connection callOnTimeout(Functor &&slot);
0097 template <typename Functor>
0098 QMetaObject::Connection callOnTimeout(const QObject *context, Functor &&slot, Qt::ConnectionType connectionType = Qt::AutoConnection);
0099 #else
0100 template <typename ... Args>
0101 QMetaObject::Connection callOnTimeout(Args && ...args)
0102 {
0103 return QObject::connect(this, &QTimer::timeout, std::forward<Args>(args)... );
0104 }
0105
0106 #endif
0107
0108 public Q_SLOTS:
0109 void start(int msec);
0110
0111 void start();
0112 void stop();
0113
0114 Q_SIGNALS:
0115 void timeout(QPrivateSignal);
0116
0117 public:
0118 void setInterval(std::chrono::milliseconds value);
0119
0120 std::chrono::milliseconds intervalAsDuration() const
0121 {
0122 return std::chrono::milliseconds(interval());
0123 }
0124
0125 std::chrono::milliseconds remainingTimeAsDuration() const
0126 {
0127 return std::chrono::milliseconds(remainingTime());
0128 }
0129
0130 #if QT_CORE_REMOVED_SINCE(6, 8)
0131 static void singleShot(std::chrono::milliseconds value, const QObject *receiver, const char *member)
0132 {
0133 singleShot(value, defaultTypeFor(value), receiver, member);
0134 }
0135 static void singleShot(std::chrono::milliseconds interval, Qt::TimerType timerType,
0136 const QObject *receiver, const char *member);
0137 #endif
0138 static void singleShot(std::chrono::nanoseconds value, const QObject *receiver, const char *member)
0139 {
0140 singleShot(value, defaultTypeFor(value), receiver, member);
0141 }
0142 static void singleShot(std::chrono::nanoseconds interval, Qt::TimerType timerType,
0143 const QObject *receiver, const char *member);
0144
0145 void start(std::chrono::milliseconds value);
0146
0147 protected:
0148 void timerEvent(QTimerEvent *) override;
0149
0150 private:
0151 Q_DISABLE_COPY(QTimer)
0152 Q_DECLARE_PRIVATE(QTimer)
0153 friend class QChronoTimer;
0154
0155 static std::chrono::nanoseconds from_msecs(std::chrono::milliseconds);
0156
0157 inline int startTimer(int){ return -1;}
0158 inline void killTimer(int){}
0159
0160 static constexpr Qt::TimerType defaultTypeFor(int msecs) noexcept
0161 { return defaultTypeFor(std::chrono::milliseconds{msecs}); }
0162
0163 #if QT_CORE_REMOVED_SINCE(6, 8)
0164 static constexpr Qt::TimerType defaultTypeFor(std::chrono::milliseconds interval) noexcept
0165 {
0166 return defaultTypeFor(std::chrono::nanoseconds{interval});
0167 }
0168 #endif
0169
0170 static constexpr Qt::TimerType defaultTypeFor(std::chrono::nanoseconds interval) noexcept
0171 {
0172
0173
0174
0175 using namespace std::chrono_literals;
0176 return interval >= 2s ? Qt::CoarseTimer : Qt::PreciseTimer;
0177 }
0178
0179 QT_CORE_INLINE_SINCE(6, 8)
0180 static void singleShotImpl(int msec, Qt::TimerType timerType,
0181 const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj);
0182
0183 #if QT_CORE_REMOVED_SINCE(6, 8)
0184 static void singleShotImpl(std::chrono::milliseconds interval, Qt::TimerType timerType,
0185 const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj);
0186 #endif
0187 static void singleShotImpl(std::chrono::nanoseconds interval, Qt::TimerType timerType,
0188 const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj);
0189 };
0190
0191 #if QT_CORE_INLINE_IMPL_SINCE(6, 8)
0192 void QTimer::singleShot(int msec, const QObject *receiver, const char *member)
0193 { singleShot(std::chrono::milliseconds{msec}, receiver, member); }
0194
0195 void QTimer::singleShot(int msec, Qt::TimerType timerType, const QObject *receiver,
0196 const char *member)
0197 { singleShot(std::chrono::milliseconds{msec}, timerType, receiver, member); }
0198
0199 void QTimer::singleShotImpl(int msec, Qt::TimerType timerType,
0200 const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj)
0201 {
0202 singleShotImpl(std::chrono::milliseconds{msec}, timerType, receiver, slotObj);
0203 }
0204 #endif
0205
0206 QT_END_NAMESPACE
0207
0208 #endif
0209
0210 #endif