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. Stop timer after 30 seconds
Forum Updated to NodeBB v4.3 + New Features

Stop timer after 30 seconds

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 602 Views 1 Watching
  • 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.
  • Dummie1138D Offline
    Dummie1138D Offline
    Dummie1138
    wrote on last edited by
    #1

    Hi. I have made a QTimer that fires once every second.

        QTimer *requestSetptTimer = new QTimer(this);
        connect(requestSetptTimer, SIGNAL(timeout()), this, SLOT(requestSetPoint()));
        connect(this, SIGNAL(reachedStableSetPoint()), requestSetptTimer, SLOT(stop())); //Disconnect on timeout or stable.
        requestSetptTimer->start(1000);
    }
    

    Right now it stops after a signal, "reachedStableSetPoint", is emitted. I would also like it to stop firing if reachedStableSetPoint is not emitted, but 30 seconds have passed. So far I have the following code.

        QTimer *requestSetptTimer = new QTimer(this);
        connect(requestSetptTimer, SIGNAL(timeout()), this, SLOT(requestSetPoint()));
        connect(this, SIGNAL(reachedStableSetPoint()), requestSetptTimer, SLOT(stop())); //Disconnect on timeout or stable.
        QTimer::singleShot(30000, requestSetptTimer, SLOT(stop()));
        requestSetptTimer->start(1000);
    

    I was wondering whether this is the standard approach, and if not, what the standard approach would be. Please let me know if more info is required.

    J.HilkJ 1 Reply Last reply
    0
    • Dummie1138D Dummie1138

      Hi. I have made a QTimer that fires once every second.

          QTimer *requestSetptTimer = new QTimer(this);
          connect(requestSetptTimer, SIGNAL(timeout()), this, SLOT(requestSetPoint()));
          connect(this, SIGNAL(reachedStableSetPoint()), requestSetptTimer, SLOT(stop())); //Disconnect on timeout or stable.
          requestSetptTimer->start(1000);
      }
      

      Right now it stops after a signal, "reachedStableSetPoint", is emitted. I would also like it to stop firing if reachedStableSetPoint is not emitted, but 30 seconds have passed. So far I have the following code.

          QTimer *requestSetptTimer = new QTimer(this);
          connect(requestSetptTimer, SIGNAL(timeout()), this, SLOT(requestSetPoint()));
          connect(this, SIGNAL(reachedStableSetPoint()), requestSetptTimer, SLOT(stop())); //Disconnect on timeout or stable.
          QTimer::singleShot(30000, requestSetptTimer, SLOT(stop()));
          requestSetptTimer->start(1000);
      

      I was wondering whether this is the standard approach, and if not, what the standard approach would be. Please let me know if more info is required.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @Dummie1138 AFAIK its either this, or starting a QElapsedTimer and checking that each execution of your requestSetPoint function


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      1
      • Cobra91151C Offline
        Cobra91151C Offline
        Cobra91151
        wrote on last edited by Cobra91151
        #3

        @Dummie1138

        Hello!

        You can make it using just a QTimer and a counter variable. Please, check out my code below:

        Code:

           int i = 0;
           QTimer *timer = new QTimer(this);
           connect(timer, &QTimer::timeout, [this, timer, i]() mutable {
               qDebug() << "Do something: " << i;
        
               if (i == 30) {
                   qDebug() << "Timout!";
                   timer->stop();
                   timer->deleteLater();
               }
        
               i++;
           });
           timer->start(1000);
        

        It runs every second. When 30 seconds have passed it displays "Timout!" and stops. Happy coding!

        JonBJ 1 Reply Last reply
        0
        • Cobra91151C Cobra91151

          @Dummie1138

          Hello!

          You can make it using just a QTimer and a counter variable. Please, check out my code below:

          Code:

             int i = 0;
             QTimer *timer = new QTimer(this);
             connect(timer, &QTimer::timeout, [this, timer, i]() mutable {
                 qDebug() << "Do something: " << i;
          
                 if (i == 30) {
                     qDebug() << "Timout!";
                     timer->stop();
                     timer->deleteLater();
                 }
          
                 i++;
             });
             timer->start(1000);
          

          It runs every second. When 30 seconds have passed it displays "Timout!" and stops. Happy coding!

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Cobra91151
          Yes, but not sure I would use this if I need that 30 seconds to be "accurate", it could exceed that more in 30 calls of timeout than one single shot or elapsed timer. Just saying, don't know if that would be an issue in practice.

          Cobra91151C 1 Reply Last reply
          0
          • JonBJ JonB

            @Cobra91151
            Yes, but not sure I would use this if I need that 30 seconds to be "accurate", it could exceed that more in 30 calls of timeout than one single shot or elapsed timer. Just saying, don't know if that would be an issue in practice.

            Cobra91151C Offline
            Cobra91151C Offline
            Cobra91151
            wrote on last edited by
            #5

            @JonB

            Hello!

            Yes, you are right reqarding QElapsedTimer, it could be more "accurate".

            Code:

             QElapsedTimer *timer = new QElapsedTimer;
             timer->start();
             while (!timer->hasExpired(30000)) {
                 qDebug() << "Elapsed: " << timer->elapsed() << " milliseconds";
             }
             delete timer;
            

            Or usage of QDeadlineTimer which counts towards a timeout in the future instead of tracking elapsed time.

               QDeadlineTimer timer(30000);
               while (!timer.hasExpired()) {
                  qDebug() << "Remaining time: " << timer.remainingTime();
               }
            

            It also could be useful solution in this case, but running 2 QTimers and one QTimer runs only to stop another I do not think it would be a good practice.

            1 Reply Last reply
            0
            • Dummie1138D Dummie1138 has marked this topic as solved on

            • Login

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