Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Timer configuration, the possibilities

Timer configuration, the possibilities

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 154 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RobertSommer
    wrote 15 days ago last edited by
    #1

    Can someone please tell me how I can configure and implement these timers in QT?
    Thanks and regards.
    ääääääää  Ashampoo_Snap_Samstag, 3. Mai 2025_17h22m34s.png

    Which timer should I use?
    OnTimer with ID or an instance of timers?

    Many thanks in advance.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Axel Spoerl
      Moderators
      wrote 15 days ago last edited by
      #6

      Something like that?

      Software Engineer
      The Qt Company, Oslo

      R 1 Reply Last reply 14 days ago
      0
      • C Offline
        C Offline
        CassD
        wrote 15 days ago last edited by
        #2

        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.

        R 1 Reply Last reply 15 days ago
        2
        • C CassD
          15 days ago

          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.

          R Offline
          R Offline
          RobertSommer
          wrote 15 days ago last edited by RobertSommer 5 Mar 2025, 18:29
          #3

          @CassD
          I need a timer

          1 -- 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!

          1 Reply Last reply
          0
          • C Offline
            C Offline
            CassD
            wrote 15 days ago last edited by CassD 5 Mar 2025, 18:37
            #4

            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.

            1 Reply Last reply
            1
            • A Offline
              A Offline
              Axel Spoerl
              Moderators
              wrote 15 days ago last edited by
              #5
              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(); });
                  }
              };
              
              

              Software Engineer
              The Qt Company, Oslo

              1 Reply Last reply
              1
              • A Offline
                A Offline
                Axel Spoerl
                Moderators
                wrote 15 days ago last edited by
                #6

                Something like that?

                Software Engineer
                The Qt Company, Oslo

                R 1 Reply Last reply 14 days ago
                0
                • A Axel Spoerl
                  15 days ago

                  Something like that?

                  R Offline
                  R Offline
                  RobertSommer
                  wrote 14 days ago last edited by
                  #7

                  @Axel-Spoerl said in Timer configuration, the possibilities:

                  Something like that?

                  OK, thanks for the sample.

                  1 Reply Last reply
                  0
                  • R RobertSommer has marked this topic as solved 14 days ago

                  5/7

                  4 May 2025, 09:02

                  • Login

                  • Login or register to search.
                  5 out of 7
                  • First post
                    5/7
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved