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. QTimer make traitement out of for loop ?
QtWS25 Last Chance

QTimer make traitement out of for loop ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
30 Posts 6 Posters 2.7k 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.
  • S Offline
    S Offline
    SGaist
    Lifetime Qt Champion
    wrote on 28 Aug 2022, 18:19 last edited by
    #10

    Based on you other thread, I would say that you should refactor your payload processing logic.

    Rather than I tight loop that you are going to block use for example invokeMethod when you are done with the processing of the current payload. That way, for that particular case, you will call invokeMethod as the last thing in you lambda to continue the processing. Your loop will otherwise continue.

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

    I 1 Reply Last reply 28 Aug 2022, 19:09
    1
    • S SGaist
      28 Aug 2022, 18:19

      Based on you other thread, I would say that you should refactor your payload processing logic.

      Rather than I tight loop that you are going to block use for example invokeMethod when you are done with the processing of the current payload. That way, for that particular case, you will call invokeMethod as the last thing in you lambda to continue the processing. Your loop will otherwise continue.

      I Offline
      I Offline
      imene
      wrote on 28 Aug 2022, 19:09 last edited by
      #11

      @SGaist good like this method preposed by @mpergand here (https://forum.qt.io/topic/138912/how-can-i-set-time-out-condition-in-the-timer-duplicated-https-forum-qt-io-topic-138913-qtimer-make-traitement-out-of-for-loop/3)

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 28 Aug 2022, 20:04 last edited by
        #12

        That's one way of implementing it.

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

        1 Reply Last reply
        0
        • I Offline
          I Offline
          imene
          wrote on 28 Aug 2022, 22:35 last edited by imene
          #13

          @SGaist is there a way more easy than that ? with another type of timer ?
          can i use this signal void QTimer::timeout()?

          M 1 Reply Last reply 28 Aug 2022, 23:41
          0
          • I imene
            28 Aug 2022, 22:35

            @SGaist is there a way more easy than that ? with another type of timer ?
            can i use this signal void QTimer::timeout()?

            M Offline
            M Offline
            mpergand
            wrote on 28 Aug 2022, 23:41 last edited by mpergand
            #14

            @imene
            For your long delay, you can use lambda as well,
            Although I don't see any real benefit with it.

            1 Reply Last reply
            0
            • I Offline
              I Offline
              imene
              wrote on 29 Aug 2022, 10:09 last edited by
              #15

              @mpergand
              What do you mean with lamba ?

              J 1 Reply Last reply 29 Aug 2022, 11:25
              0
              • I imene
                29 Aug 2022, 10:09

                @mpergand
                What do you mean with lamba ?

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 29 Aug 2022, 11:25 last edited by
                #16

                @imene C++ lambda functions: https://en.cppreference.com/w/cpp/language/lambda

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

                1 Reply Last reply
                1
                • I Offline
                  I Offline
                  imene
                  wrote on 29 Aug 2022, 13:34 last edited by
                  #17

                  QTimer::singleShot :This static function calls a slot after a given time interval; in my case i want the code to make the traitement inside QTimer::singleShot before it pass to make the next if condition .

                  C 1 Reply Last reply 29 Aug 2022, 13:35
                  0
                  • I imene
                    29 Aug 2022, 13:34

                    QTimer::singleShot :This static function calls a slot after a given time interval; in my case i want the code to make the traitement inside QTimer::singleShot before it pass to make the next if condition .

                    C Online
                    C Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 29 Aug 2022, 13:35 last edited by
                    #18

                    @imene said in QTimer make traitement out of for loop ?:

                    to make the traitement inside QTimer::singleShot before it pass to make the next if condition .

                    Let the QTimer::singleShot() fire and check for your codition inside the slot.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      imene
                      wrote on 29 Aug 2022, 13:47 last edited by
                      #19

                      @Christian-Ehrlicher how ?

                      C 1 Reply Last reply 29 Aug 2022, 13:54
                      0
                      • I imene
                        29 Aug 2022, 13:47

                        @Christian-Ehrlicher how ?

                        C Online
                        C Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on 29 Aug 2022, 13:54 last edited by Christian Ehrlicher
                        #20

                        @imene said in QTimer make traitement out of for loop ?:

                        how ?

                        if (yourCoditionIsMet) {
                          doSomethingYouWant()
                        }
                        

                        maybe?

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        0
                        • I Offline
                          I Offline
                          imene
                          wrote on 29 Aug 2022, 14:34 last edited by
                          #21

                          @Christian-Ehrlicher
                          my condition is to wait for QTimer::singleShot() to be executed

                          C 1 Reply Last reply 29 Aug 2022, 14:39
                          0
                          • I imene
                            29 Aug 2022, 14:34

                            @Christian-Ehrlicher
                            my condition is to wait for QTimer::singleShot() to be executed

                            C Online
                            C Online
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on 29 Aug 2022, 14:39 last edited by
                            #22

                            @imene said in QTimer make traitement out of for loop ?:

                            my condition is to wait for QTimer::singleShot() to be executed

                            Why?
                            If you want to stop a timer, use a QTimer object and stop it if you want.

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            1 Reply Last reply
                            0
                            • I Offline
                              I Offline
                              imene
                              wrote on 29 Aug 2022, 16:47 last edited by
                              #23

                              This timer

                              connect(m_speedTimer, &QTimer::timeout, this, &MainWindow::on_Send_Row_Button_clicked);
                              
                              
                               if( trameRecu[0]==trameTest[0]){
                                              qDebug() <<"Test FF good!"; //model dialogue informing
                                              mModel->item(b, 4)->setText("FF");//ack
                                             
                              
                                              m_speedTimer->start(8000);
                              
                              
                                                     QTimer::singleShot(7000, [=](){
                                                  qDebug()<<"Timer on do something after 7000 msec...";
                              
                              
                                              if((Send_Frame_Get_ACK_Function(SetPayloadRPM))=="FF")
                              
                                              { qDebug()  << "Send_Frame_Get_ACK_Function Pass" ;
                              
                                                  QString MySpeed=0;
                              
                                                MySpeed = Send_Frame_Get_Speed_Function(NSetPayload); //SetPayloadMem="F504"
                                                qDebug()  << " MySpeed" <<MySpeed;
                              
                                                double MSpeed = MySpeed.toFloat();
                                                  qDebug()  << " MSpeed" <<MSpeed;
                                                bool result = ((MSpeed >= 40) && (MSpeed <= 60));
                                                        qDebug() << "((MSpeed >= 40) && (MSpeed <= 60))" << result;
                                                        result = ((MSpeed >= 90) && (MSpeed <= 110)  );
                                                        qDebug() << "((MSpeed >= 90) && (MSpeed <= 110))" << result;
                                                        result = ((MSpeed >= 490) && (MSpeed <= 520)  );
                                                        qDebug() << "((MSpeed >= 490) && (MSpeed <= 520))" << result;
                              
                                                if ((MSpeed > 40) && (MSpeed < 60)  )
                              
                                                {mModel->item(b, 6)->setText("+50");}//result
                              
                                                else if ((MSpeed > 90) && (MSpeed < 110))
                                                {mModel->item(b, 6)->setText("+100");}//result
                              
                                                else if ((MSpeed > 140) && (MSpeed < 160))
                                                {mModel->item(b, 6)->setText("+150");}//result
                              
                                                else if ((MSpeed > 190) && (MSpeed < 210))
                                                {mModel->item(b, 6)->setText("+200");}//result
                              
                                                else if ((MSpeed > 240) && (MSpeed < 260))
                                                {mModel->item(b, 6)->setText("+100");}//result
                              
                                                else if ((MSpeed > 290) && (MSpeed < 310))
                                                {mModel->item(b, 6)->setText("+100");}//result
                              
                              
                                                else if ((MSpeed >490) && (MSpeed < 520))
                                                {mModel->item(b, 6)->setText("+500");}//result
                                                else
                                                {mModel->item(b, 6)->setText("Error");
                                                qDebug()  << " MSpeed if" <<MSpeed;}//result
                              
                              
                                              }
                              
                                              m_speedTimer->stop();
                              
                                             });//singleshot timer here
                              
                                          }
                              

                              m_speedTimer->start(8000);
                              repeat the traitement under it each 8000 but when i use m_speedTimer->stop(); it make traitement one time and stops without waiting 8000 msec!
                              I need a timer that make traitement after 8000msec.

                              C 1 Reply Last reply 29 Aug 2022, 16:50
                              0
                              • I imene
                                29 Aug 2022, 16:47

                                This timer

                                connect(m_speedTimer, &QTimer::timeout, this, &MainWindow::on_Send_Row_Button_clicked);
                                
                                
                                 if( trameRecu[0]==trameTest[0]){
                                                qDebug() <<"Test FF good!"; //model dialogue informing
                                                mModel->item(b, 4)->setText("FF");//ack
                                               
                                
                                                m_speedTimer->start(8000);
                                
                                
                                                       QTimer::singleShot(7000, [=](){
                                                    qDebug()<<"Timer on do something after 7000 msec...";
                                
                                
                                                if((Send_Frame_Get_ACK_Function(SetPayloadRPM))=="FF")
                                
                                                { qDebug()  << "Send_Frame_Get_ACK_Function Pass" ;
                                
                                                    QString MySpeed=0;
                                
                                                  MySpeed = Send_Frame_Get_Speed_Function(NSetPayload); //SetPayloadMem="F504"
                                                  qDebug()  << " MySpeed" <<MySpeed;
                                
                                                  double MSpeed = MySpeed.toFloat();
                                                    qDebug()  << " MSpeed" <<MSpeed;
                                                  bool result = ((MSpeed >= 40) && (MSpeed <= 60));
                                                          qDebug() << "((MSpeed >= 40) && (MSpeed <= 60))" << result;
                                                          result = ((MSpeed >= 90) && (MSpeed <= 110)  );
                                                          qDebug() << "((MSpeed >= 90) && (MSpeed <= 110))" << result;
                                                          result = ((MSpeed >= 490) && (MSpeed <= 520)  );
                                                          qDebug() << "((MSpeed >= 490) && (MSpeed <= 520))" << result;
                                
                                                  if ((MSpeed > 40) && (MSpeed < 60)  )
                                
                                                  {mModel->item(b, 6)->setText("+50");}//result
                                
                                                  else if ((MSpeed > 90) && (MSpeed < 110))
                                                  {mModel->item(b, 6)->setText("+100");}//result
                                
                                                  else if ((MSpeed > 140) && (MSpeed < 160))
                                                  {mModel->item(b, 6)->setText("+150");}//result
                                
                                                  else if ((MSpeed > 190) && (MSpeed < 210))
                                                  {mModel->item(b, 6)->setText("+200");}//result
                                
                                                  else if ((MSpeed > 240) && (MSpeed < 260))
                                                  {mModel->item(b, 6)->setText("+100");}//result
                                
                                                  else if ((MSpeed > 290) && (MSpeed < 310))
                                                  {mModel->item(b, 6)->setText("+100");}//result
                                
                                
                                                  else if ((MSpeed >490) && (MSpeed < 520))
                                                  {mModel->item(b, 6)->setText("+500");}//result
                                                  else
                                                  {mModel->item(b, 6)->setText("Error");
                                                  qDebug()  << " MSpeed if" <<MSpeed;}//result
                                
                                
                                                }
                                
                                                m_speedTimer->stop();
                                
                                               });//singleshot timer here
                                
                                            }
                                

                                m_speedTimer->start(8000);
                                repeat the traitement under it each 8000 but when i use m_speedTimer->stop(); it make traitement one time and stops without waiting 8000 msec!
                                I need a timer that make traitement after 8000msec.

                                C Online
                                C Online
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on 29 Aug 2022, 16:50 last edited by
                                #24

                                @imene said in QTimer make traitement out of for loop ?:

                                t make traitement one time and stops without waiting 8000 msec!

                                Then remember that you want to stop your timer after the next timeout and stop it then.

                                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                Visit the Qt Academy at https://academy.qt.io/catalog

                                1 Reply Last reply
                                0
                                • I Offline
                                  I Offline
                                  imene
                                  wrote on 29 Aug 2022, 17:04 last edited by
                                  #25

                                  What does m_speedTimer->start(8000); and m_speedTimer->stop(); do exactely ?

                                  C 1 Reply Last reply 29 Aug 2022, 17:05
                                  0
                                  • I imene
                                    29 Aug 2022, 17:04

                                    What does m_speedTimer->start(8000); and m_speedTimer->stop(); do exactely ?

                                    C Online
                                    C Online
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on 29 Aug 2022, 17:05 last edited by Christian Ehrlicher
                                    #26

                                    @imene said in QTimer make traitement out of for loop ?:

                                    What does m_speedTimer->start(8000); and m_speedTimer->stop(); do exactely ?

                                    Really??

                                    RTM https://doc.qt.io/qt-6/qtimer.html

                                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                    Visit the Qt Academy at https://academy.qt.io/catalog

                                    1 Reply Last reply
                                    1
                                    • I Offline
                                      I Offline
                                      imene
                                      wrote on 29 Aug 2022, 17:12 last edited by imene
                                      #27

                                      I want a timer that stop reading the code for 7sec then complete and execute the function, because singleShot timer execute function after 7 sec here:

                                      QTimer::singleShot(7000, [=](){
                                                          qDebug()<<"Timer on do something after 6 second...";
                                      
                                      
                                                     // i want to set a time out condition here
                                      
                                                     });//timer ends here
                                      
                                      C 1 Reply Last reply 29 Aug 2022, 17:55
                                      0
                                      • I imene
                                        29 Aug 2022, 17:12

                                        I want a timer that stop reading the code for 7sec then complete and execute the function, because singleShot timer execute function after 7 sec here:

                                        QTimer::singleShot(7000, [=](){
                                                            qDebug()<<"Timer on do something after 6 second...";
                                        
                                        
                                                       // i want to set a time out condition here
                                        
                                                       });//timer ends here
                                        
                                        C Online
                                        C Online
                                        Christian Ehrlicher
                                        Lifetime Qt Champion
                                        wrote on 29 Aug 2022, 17:55 last edited by
                                        #28

                                        @imene said in QTimer make traitement out of for loop ?:

                                        qDebug()<<"Timer on do something after 6 second...";

                                        QTimer::singleShot(7000, ={

                                        Maybe you should at least add a correct comment...

                                        We already told you how to start a timer, how to stop it and how you set a (member) variable to not execute something if this variable is set to a certain value is nothing we need to discuss here - it's a task for a c++ beginners book.

                                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                        Visit the Qt Academy at https://academy.qt.io/catalog

                                        1 Reply Last reply
                                        1
                                        • I Offline
                                          I Offline
                                          imene
                                          wrote on 31 Aug 2022, 22:49 last edited by
                                          #29

                                          what i have to do ?

                                          QObject::connect(m_speedTimer,&QTimer::timeout, this, &MainWindow::sendFrame,Qt::AutoConnection);
                                          
                                          void MainWindow::sendFrame(QString IdData_R,QString PayloadData_R,int i)
                                          {
                                          
                                                      if (m_canDevice)
                                                  {   m_speedTimer->start(6000);
                                                  ...
                                                   }
                                          
                                          }
                                          
                                          void MainWindow::on_SendAll_Button_clicked()
                                          {int a= m_ui->Table_CSV->model()->rowCount();
                                           for(int i=0;i<a; i++) //R :read , W :write
                                              { sendFrame(IdData_R, PayloadData_R,i);//});
                                               }
                                              
                                              m_speedTimer->stop();
                                              
                                           }
                                          

                                          513347e1-0444-4877-aa5d-fbcd337e5af2-image.png

                                          M 1 Reply Last reply 31 Aug 2022, 23:26
                                          0

                                          19/30

                                          29 Aug 2022, 13:47

                                          • Login

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