Timer configuration, the possibilities
-
Can someone please tell me how I can configure and implement these timers in QT?
Thanks and regards.
Which timer should I use?
OnTimer with ID or an instance of timers?Many thanks in advance.
-
Something like that?
-
do you want a timer that returns something that switches between a two state output ?
this is not supporting out of the box. On your third graph, the isRunning state is actually permanently on. All that QTimer does, is provide a signal (that is a single "pulse") when the timer timesout. Yours to trigger your state change.
If what you want is such a twin (or multi) state triggered by timer, you need to implement this by yourself. If time up and time down are the same period, all you need to do is switch the state on every trigger and set the interval to half the period.
If you want different up and down periods, you will need either two timers and sync them, or (probably better option) reinitialize the interval property at every trigger. You also might want to take a look at QTimer::TimerType property depending on desired accuracy. -
do you want a timer that returns something that switches between a two state output ?
this is not supporting out of the box. On your third graph, the isRunning state is actually permanently on. All that QTimer does, is provide a signal (that is a single "pulse") when the timer timesout. Yours to trigger your state change.
If what you want is such a twin (or multi) state triggered by timer, you need to implement this by yourself. If time up and time down are the same period, all you need to do is switch the state on every trigger and set the interval to half the period.
If you want different up and down periods, you will need either two timers and sync them, or (probably better option) reinitialize the interval property at every trigger. You also might want to take a look at QTimer::TimerType property depending on desired accuracy.@CassD
I need a timer1 -- StartTimer --> After 10 seconds the timer is running triggered with 5000ms and stop it
2 -- StartTimer --> Immediately active, triggered after a timeout of 5 seconds ad stop it.
3 -- StartTimer --> Starts after 10 seconds and repeats periodically, triggering, wait 10 seconds until start, trigger after 5 seconds.
Can you give an example? Thank you!
-
the second case is straightforward. for first and third looks like you need a second timer to start the first one. or conversely.
if you need a timer that repeats at every interval, you need the singleShot property to false. If you want it to trigger once, singleShot shall be true. -
class Timer : public QObject { Q_OBJECT public: enum Type { Delayed, Permanent, Blinker, }; Q_ENUM(Type) enum State { On, Off, }; Q_ENUM(State) Timer(Type type) : m_type(type) {} State state() const { return m_state; } public slots: void stop() { setState(State::Off); m_blinking = false; } void start() { setState(State::Off); switch (m_type) { case Delayed: QTimer::singleShot(10000, this, [&]{ setState(State::On); }); QTimer::singleShot(15000, this, [&]{ setState(State::Off); }); break; case Permanent: setState(State::On); QTimer::singleShot(15000, this, [&]{ setState(State::Off); }); break; case Blinker: m_blinking = true; QTimer::singleShot(10000, this, [&]{ toggleState(); }); break; } } signals: void stateChanged(State state); private: State m_state = State::Off; const Type m_type; bool m_blinking = false; void setState(State state) { if (m_state == state) return; m_state = state; emit stateChanged(state); } void toggleState() { if (!m_blinking) return; setState(state() == State::Off ? State::On : State::Off); QTimer::singleShot(10000, this, [&]{ toggleState(); }); } };
-
Something like that?
-
Something like that?
@Axel-Spoerl said in Timer configuration, the possibilities:
Something like that?
OK, thanks for the sample.
-