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. Wait for the next action

Wait for the next action

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 2.3k Views 2 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.
  • M Offline
    M Offline
    maratk1n
    wrote on last edited by maratk1n
    #1

    Hi all!

    Tell me, please, how can I make a delay before the next action. In this case, I still need to update the progress bar.

    void ClassName::start()
    {
        action1();
    
        /*
        
        Here I need to wait a while, update the progress bar, and go further
    
        */
    
        action2();
        emit finished();
    }
    

    I tried to do it this way, but it does not work.

    void ClassName::start()
    {
        emit mainLabelEmit(QString("start"));
        QTimer *timer = new QTimer(this);
        timer->setSingleShot(true);
        timerCount = 10;
        //timer = new QTimer();
        timer->start(timerCount * 1000);
        while (timer->remainingTime() >= 0)
        {
            QString smallLabel = QString("Left %1 sec").arg(timer->remainingTime() / 1000);
            //qDebug() << smallLabel;
            emit smallLabelEmit(smallLabel);
            if (timer->remainingTime() == 0)
            {
                timer->stop();
                emit smallLabelEmit(QString(""));
            }
            else
            {
                emit progressBarEmit(100 - 100 * timer->remainingTime() / timerCount);
            }
        }
    
        for (int i = 0; i < 7; i++)
        {
            emit open(i);
        }
        for (int i = 0; i < 7; i++)
        {
            emit close(i);
        }
        emit finished();
    }
    

    Thanks!

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      Using a timer is a good idea but you should restructure the code so the timer slot can handle the the actions
      Also do not use long for loop or while loop in Qt programs as it strangulate the event loop and make app not draw/seem to lag etc.

      Say u set the timer to 1 sec let it call timerslot (repetitive , not single slot)
      and declare a long int as tick. ( as member in class)
      tick=0;
      timer->start

      void timerslot() {

      if( tick ==0 )
      Call action 1; // called at once
      else if ( tick == 5 ) // 5 secs
      Call Action 2

      // update bar if needed
      ...

      tick++; // progress "time"
      }

      A better design would would involve action to signal its finished so then you run action 2

      But for a fast fix, use the timer to count until next action should be performed.
      Its not possible to wait in the main thread as you would then stop whole application from drawing.
      So using timer, you are not blocking it and the wait its done by checking for time (ticks) and perform
      action after X ticks.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        What kind of action do you have in mind ? What's the workflow of your application ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        M 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          What kind of action do you have in mind ? What's the workflow of your application ?

          M Offline
          M Offline
          maratk1n
          wrote on last edited by maratk1n
          #4

          @SGaist said in Wait for the next action:

          Hi,

          What kind of action do you have in mind ? What's the workflow of your application ?

          Hello,
          I have an application for filling gas barrels. I open / close the valve, then I want to wait a few seconds to stabilize. After waiting, I open the drain valve.

          jsulmJ 1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Then you should rather model that with a state machine. That would make more sense from a handling point of view.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            M 1 Reply Last reply
            2
            • M maratk1n

              @SGaist said in Wait for the next action:

              Hi,

              What kind of action do you have in mind ? What's the workflow of your application ?

              Hello,
              I have an application for filling gas barrels. I open / close the valve, then I want to wait a few seconds to stabilize. After waiting, I open the drain valve.

              jsulmJ Online
              jsulmJ Online
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @maratk1n Here the link to what @SGaist mentioned: http://doc.qt.io/qt-5.8/statemachine-api.html

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • SGaistS SGaist

                Then you should rather model that with a state machine. That would make more sense from a handling point of view.

                M Offline
                M Offline
                maratk1n
                wrote on last edited by
                #7

                @SGaist
                @jsulm
                Thank you very much! I will sort this out :)

                1 Reply Last reply
                0

                • Login

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