File indexing completed on 2025-01-18 10:07:40
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
0035 void setInterval(int msec);
0036 int interval() const;
0037 QBindable<int> bindableInterval();
0038
0039 int remainingTime() const;
0040
0041 void setTimerType(Qt::TimerType atype);
0042 Qt::TimerType timerType() const;
0043 QBindable<Qt::TimerType> bindableTimerType();
0044
0045 void setSingleShot(bool singleShot);
0046 bool isSingleShot() const;
0047 QBindable<bool> bindableSingleShot();
0048
0049 static void singleShot(int msec, const QObject *receiver, const char *member);
0050 static void singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, const char *member);
0051
0052
0053 #ifdef Q_QDOC
0054 template <typename Duration, typename Functor>
0055 static inline void singleShot(Duration interval, const QObject *receiver, Functor &&slot);
0056 template <typename Duration, typename Functor>
0057 static inline void singleShot(Duration interval, Qt::TimerType timerType,
0058 const QObject *receiver, Functor &&slot);
0059 #else
0060 template <typename Duration, typename Functor>
0061 static inline void singleShot(Duration interval,
0062 const typename QtPrivate::ContextTypeForFunctor<Functor>::ContextType *receiver,
0063 Functor &&slot)
0064 {
0065 singleShot(interval, defaultTypeFor(interval), receiver, std::forward<Functor>(slot));
0066 }
0067 template <typename Duration, typename Functor>
0068 static inline void singleShot(Duration interval, Qt::TimerType timerType,
0069 const typename QtPrivate::ContextTypeForFunctor<Functor>::ContextType *receiver,
0070 Functor &&slot)
0071 {
0072 using Prototype = void(*)();
0073 singleShotImpl(interval, timerType, receiver,
0074 QtPrivate::makeCallableObject<Prototype>(std::forward<Functor>(slot)));
0075 }
0076 #endif
0077
0078
0079 template <typename Duration, typename Functor>
0080 static inline void singleShot(Duration interval, Functor &&slot)
0081 {
0082 singleShot(interval, defaultTypeFor(interval), nullptr, std::forward<Functor>(slot));
0083 }
0084 template <typename Duration, typename Functor>
0085 static inline void singleShot(Duration interval, Qt::TimerType timerType, Functor &&slot)
0086 {
0087 singleShot(interval, timerType, nullptr, std::forward<Functor>(slot));
0088 }
0089
0090 #ifdef Q_QDOC
0091 template <typename Functor>
0092 QMetaObject::Connection callOnTimeout(Functor &&slot);
0093 template <typename Functor>
0094 QMetaObject::Connection callOnTimeout(const QObject *context, Functor &&slot, Qt::ConnectionType connectionType = Qt::AutoConnection);
0095 #else
0096 template <typename ... Args>
0097 QMetaObject::Connection callOnTimeout(Args && ...args)
0098 {
0099 return QObject::connect(this, &QTimer::timeout, std::forward<Args>(args)... );
0100 }
0101
0102 #endif
0103
0104 public Q_SLOTS:
0105 void start(int msec);
0106
0107 void start();
0108 void stop();
0109
0110 Q_SIGNALS:
0111 void timeout(QPrivateSignal);
0112
0113 public:
0114 void setInterval(std::chrono::milliseconds value)
0115 {
0116 setInterval(int(value.count()));
0117 }
0118
0119 std::chrono::milliseconds intervalAsDuration() const
0120 {
0121 return std::chrono::milliseconds(interval());
0122 }
0123
0124 std::chrono::milliseconds remainingTimeAsDuration() const
0125 {
0126 return std::chrono::milliseconds(remainingTime());
0127 }
0128
0129 static void singleShot(std::chrono::milliseconds value, const QObject *receiver, const char *member)
0130 {
0131 singleShot(int(value.count()), receiver, member);
0132 }
0133
0134 static void singleShot(std::chrono::milliseconds value, Qt::TimerType timerType, const QObject *receiver, const char *member)
0135 {
0136 singleShot(int(value.count()), timerType, receiver, member);
0137 }
0138
0139 void start(std::chrono::milliseconds value)
0140 {
0141 start(int(value.count()));
0142 }
0143
0144 protected:
0145 void timerEvent(QTimerEvent *) override;
0146
0147 private:
0148 Q_DISABLE_COPY(QTimer)
0149 Q_DECLARE_PRIVATE(QTimer)
0150
0151 inline int startTimer(int){ return -1;}
0152 inline void killTimer(int){}
0153
0154 static constexpr Qt::TimerType defaultTypeFor(int msecs) noexcept
0155 { return defaultTypeFor(std::chrono::milliseconds{msecs}); }
0156
0157 static constexpr Qt::TimerType defaultTypeFor(std::chrono::milliseconds interval) noexcept
0158 {
0159
0160
0161
0162 using namespace std::chrono_literals;
0163 return interval >= 2s ? Qt::CoarseTimer : Qt::PreciseTimer;
0164 }
0165
0166 static void singleShotImpl(int msec, Qt::TimerType timerType,
0167 const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj);
0168
0169 static void singleShotImpl(std::chrono::milliseconds interval, Qt::TimerType timerType,
0170 const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj)
0171 {
0172 singleShotImpl(int(interval.count()),
0173 timerType, receiver, slotObj);
0174 }
0175 };
0176
0177 QT_END_NAMESPACE
0178
0179 #endif
0180
0181 #endif