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. How to use two or three button's logic in single button using CPP in QT
Forum Updated to NodeBB v4.3 + New Features

How to use two or three button's logic in single button using CPP in QT

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 5 Posters 1.4k 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.
  • R Offline
    R Offline
    reddipradeep.embd
    wrote on 10 Feb 2020, 19:58 last edited by
    #1

    The code has written in CPP using QT and it has multiple buttons (Say A, B and C). If we press A or B or C, it executes its own commands as per their logic. Here I want to add one extra button (Say D), if I press that button D, it should execute operation of A button followed by B button and then followed by C button.

    Can you please help me, as I am beginner to QT application. I have cut and pasted the code written under button A, B and C to the button D, Now its behaving differently, I don't know what operation its executing (A or B or C or all combined), when I cut and paste only A button commands, the new button D is executing the commands of A, but here I want to execute all one by one in sequence.

    Please help me

    J 1 Reply Last reply 10 Feb 2020, 20:01
    0
    • R reddipradeep.embd
      10 Feb 2020, 19:58

      The code has written in CPP using QT and it has multiple buttons (Say A, B and C). If we press A or B or C, it executes its own commands as per their logic. Here I want to add one extra button (Say D), if I press that button D, it should execute operation of A button followed by B button and then followed by C button.

      Can you please help me, as I am beginner to QT application. I have cut and pasted the code written under button A, B and C to the button D, Now its behaving differently, I don't know what operation its executing (A or B or C or all combined), when I cut and paste only A button commands, the new button D is executing the commands of A, but here I want to execute all one by one in sequence.

      Please help me

      J Offline
      J Offline
      JonB
      wrote on 10 Feb 2020, 20:01 last edited by
      #2

      @reddipradeep-embd
      You have to show code.

      1 Reply Last reply
      2
      • R Offline
        R Offline
        reddipradeep.embd
        wrote on 10 Feb 2020, 20:13 last edited by
        #3

        if(ui->radioButton_wake->isChecked()) // Button A
        {
        ui->label_lastCommand->setText("Last command: Wake");
        ui->progressBar->setMaximum(UPDATE_FREQUENCY * 122);
        pJMPX->RDS_Set_11A_Enable(true);
        progressTimer->start();
        }
        else if(ui->radioButton_beep->isChecked()) // Button B
        {
        ui->label_lastCommand->setText("Last command: Beep");
        ui->progressBar->setMaximum(UPDATE_FREQUENCY * (ui->doubleSpinBox_general->value() + 122));
        pJMPX->RDS_Set_11A_Enable(true);
        progressTimer->start();

            QTimer::singleShot(122000, this, [this](){
                pJMPX->RDS_Set_11A_data(0x0008, 0x0000);
            });
        }
        else if(ui->radioButton_beginPlayback->isChecked())  // Button c
        {
            ui->label_lastCommand->setText("Last command: Begin Playback");
            ui->progressBar->setMaximum(UPDATE_FREQUENCY * (ui->doubleSpinBox_general->value() + 122));
            pJMPX->RDS_Set_11A_Enable(true);
            progressTimer->start();
        
            QTimer::singleShot(122000, this, [this](){
                pJMPX->RDS_Set_11A_data(0x0005, 0x0000);
            });
        }
        else if(ui->radioButton->isChecked())
        {
        

        Code to execute operations of button A, B and C ( one after one ) ?

        }
        
        1 Reply Last reply
        0
        • P Offline
          P Offline
          presstab
          wrote on 11 Feb 2020, 01:52 last edited by
          #4

          Refactor each scope into a method of the class. This is more programming 101, than anything to do with Qt though. This is some incomplete code to give an idea of how you would go about it. You would also need to add method declarations to your header file.

          MyWidget::DoA()
          {
            ui->label_lastCommand->setText("Last command: Wake");
            ui->progressBar->setMaximum(UPDATE_FREQUENCY * 122);
            pJMPX->RDS_Set_11A_Enable(true);
            progressTimer->start();
          }
          
          MyWidget::DoB()
          {
            ui->label_lastCommand->setText("Last command: Beep");
            ui->progressBar->setMaximum(UPDATE_FREQUENCY * (ui->doubleSpinBox_general->value() + 122));
            pJMPX->RDS_Set_11A_Enable(true);
            progressTimer->start();
          
            QTimer::singleShot(122000, this, [this](){
                  pJMPX->RDS_Set_11A_data(0x0008, 0x0000);
              });
          }
          
          MyWidget::DoC()
          {
              ui->label_lastCommand->setText("Last command: Begin Playback");
              ui->progressBar->setMaximum(UPDATE_FREQUENCY * (ui->doubleSpinBox_general->value() + 122));
              pJMPX->RDS_Set_11A_Enable(true);
              progressTimer->start();
          
              QTimer::singleShot(122000, this, [this](){
                  pJMPX->RDS_Set_11A_data(0x0005, 0x0000);
              });
          }
          
          
          MyWidget::NameOfYourSlot()
          {
          
          if(ui->radioButton_wake->isChecked()) // Button A
          {
            DoA();
          }
          else if(ui->radioButton_beep->isChecked()) // Button B
          {
            DoB();
          }
          else if(ui->radioButton_beginPlayback->isChecked())  // Button c
          {
              DoC();
          }
          else if(ui->radioButton->isChecked())
          {
            DoA();
            DoB();
            DoC();
          
          }
          
          }
          
          
          F 1 Reply Last reply 11 Feb 2020, 15:52
          5
          • P presstab
            11 Feb 2020, 01:52

            Refactor each scope into a method of the class. This is more programming 101, than anything to do with Qt though. This is some incomplete code to give an idea of how you would go about it. You would also need to add method declarations to your header file.

            MyWidget::DoA()
            {
              ui->label_lastCommand->setText("Last command: Wake");
              ui->progressBar->setMaximum(UPDATE_FREQUENCY * 122);
              pJMPX->RDS_Set_11A_Enable(true);
              progressTimer->start();
            }
            
            MyWidget::DoB()
            {
              ui->label_lastCommand->setText("Last command: Beep");
              ui->progressBar->setMaximum(UPDATE_FREQUENCY * (ui->doubleSpinBox_general->value() + 122));
              pJMPX->RDS_Set_11A_Enable(true);
              progressTimer->start();
            
              QTimer::singleShot(122000, this, [this](){
                    pJMPX->RDS_Set_11A_data(0x0008, 0x0000);
                });
            }
            
            MyWidget::DoC()
            {
                ui->label_lastCommand->setText("Last command: Begin Playback");
                ui->progressBar->setMaximum(UPDATE_FREQUENCY * (ui->doubleSpinBox_general->value() + 122));
                pJMPX->RDS_Set_11A_Enable(true);
                progressTimer->start();
            
                QTimer::singleShot(122000, this, [this](){
                    pJMPX->RDS_Set_11A_data(0x0005, 0x0000);
                });
            }
            
            
            MyWidget::NameOfYourSlot()
            {
            
            if(ui->radioButton_wake->isChecked()) // Button A
            {
              DoA();
            }
            else if(ui->radioButton_beep->isChecked()) // Button B
            {
              DoB();
            }
            else if(ui->radioButton_beginPlayback->isChecked())  // Button c
            {
                DoC();
            }
            else if(ui->radioButton->isChecked())
            {
              DoA();
              DoB();
              DoC();
            
            }
            
            }
            
            
            F Offline
            F Offline
            fcarney
            wrote on 11 Feb 2020, 15:52 last edited by
            #5

            @presstab The problem is those timers. The functions should be turned into slots. Then the methods need to be chained as each one completes. The asynchronous nature of each function makes this complicated.

            C++ is a perfectly valid school of magic.

            P 1 Reply Last reply 11 Feb 2020, 20:21
            0
            • F fcarney
              11 Feb 2020, 15:52

              @presstab The problem is those timers. The functions should be turned into slots. Then the methods need to be chained as each one completes. The asynchronous nature of each function makes this complicated.

              P Offline
              P Offline
              presstab
              wrote on 11 Feb 2020, 20:21 last edited by
              #6

              @fcarney correct. Overall the code isn't fantastic. Looking at it, I don't see why there would be any purpose of running A at all if immediately running B and C after (although not totally sure since we don't have that code) . Although cleaning up the code completely is always the best, this would likely still accomplish the goal.

              MyWidget::DoB(bool fRunC)
              {
                   ui->label_lastCommand->setText("Last command: Beep");
                ui->progressBar->setMaximum(UPDATE_FREQUENCY * (ui->doubleSpinBox_general->value() + 122));
                pJMPX->RDS_Set_11A_Enable(true);
                progressTimer->start();
              
                QTimer::singleShot(122000, this, [this](){
                      pJMPX->RDS_Set_11A_data(0x0008, 0x0000);
                      if (fRunC)
                          DoC();
                  });
              }
              
              MyWidget::NameOfYourSlot()
              {
              
              if(ui->radioButton_wake->isChecked()) // Button A
              {
                DoA();
              }
              else if(ui->radioButton_beep->isChecked()) // Button B
              {
                DoB(/*fRunC*/false);
              }
              else if(ui->radioButton_beginPlayback->isChecked())  // Button c
              {
                  DoC();
              }
              else if(ui->radioButton->isChecked())
              {
                DoB(/*fRunC*/true);
              }
              
              R 1 Reply Last reply 12 Feb 2020, 21:25
              0
              • P presstab
                11 Feb 2020, 20:21

                @fcarney correct. Overall the code isn't fantastic. Looking at it, I don't see why there would be any purpose of running A at all if immediately running B and C after (although not totally sure since we don't have that code) . Although cleaning up the code completely is always the best, this would likely still accomplish the goal.

                MyWidget::DoB(bool fRunC)
                {
                     ui->label_lastCommand->setText("Last command: Beep");
                  ui->progressBar->setMaximum(UPDATE_FREQUENCY * (ui->doubleSpinBox_general->value() + 122));
                  pJMPX->RDS_Set_11A_Enable(true);
                  progressTimer->start();
                
                  QTimer::singleShot(122000, this, [this](){
                        pJMPX->RDS_Set_11A_data(0x0008, 0x0000);
                        if (fRunC)
                            DoC();
                    });
                }
                
                MyWidget::NameOfYourSlot()
                {
                
                if(ui->radioButton_wake->isChecked()) // Button A
                {
                  DoA();
                }
                else if(ui->radioButton_beep->isChecked()) // Button B
                {
                  DoB(/*fRunC*/false);
                }
                else if(ui->radioButton_beginPlayback->isChecked())  // Button c
                {
                    DoC();
                }
                else if(ui->radioButton->isChecked())
                {
                  DoB(/*fRunC*/true);
                }
                
                R Offline
                R Offline
                reddipradeep.embd
                wrote on 12 Feb 2020, 21:25 last edited by
                #7

                @presstab said in How to use two or three button's logic in single button using CPP in QT:

                DoB(/fRunC/true);

                Thanks for the response, I have used same logic and getting the error " Variable 'fRunC' cannot be implicitly captured in a lambda with no capture-default specified". Could you please resolve the issue

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  fcarney
                  wrote on 12 Feb 2020, 21:56 last edited by
                  #8

                  @reddipradeep-embd said in How to use two or three button's logic in single button using CPP in QT:

                  cannot be implicitly captured in a lambda

                  https://lmgtfy.com/?q=lambda+capture

                  C++ is a perfectly valid school of magic.

                  P 1 Reply Last reply 13 Feb 2020, 19:05
                  1
                  • F fcarney
                    12 Feb 2020, 21:56

                    @reddipradeep-embd said in How to use two or three button's logic in single button using CPP in QT:

                    cannot be implicitly captured in a lambda

                    https://lmgtfy.com/?q=lambda+capture

                    P Offline
                    P Offline
                    presstab
                    wrote on 13 Feb 2020, 19:05 last edited by
                    #9

                    @fcarney

                      QTimer::singleShot(122000, this, [fRunC](){
                            pJMPX->RDS_Set_11A_data(0x0008, 0x0000);
                            if (fRunC)
                                DoC();
                        });
                    
                    F R 2 Replies Last reply 13 Feb 2020, 19:17
                    0
                    • P presstab
                      13 Feb 2020, 19:05

                      @fcarney

                        QTimer::singleShot(122000, this, [fRunC](){
                              pJMPX->RDS_Set_11A_data(0x0008, 0x0000);
                              if (fRunC)
                                  DoC();
                          });
                      
                      F Offline
                      F Offline
                      fcarney
                      wrote on 13 Feb 2020, 19:17 last edited by
                      #10

                      @presstab I was trying to lead the op to water.

                      C++ is a perfectly valid school of magic.

                      1 Reply Last reply
                      1
                      • P presstab
                        13 Feb 2020, 19:05

                        @fcarney

                          QTimer::singleShot(122000, this, [fRunC](){
                                pJMPX->RDS_Set_11A_data(0x0008, 0x0000);
                                if (fRunC)
                                    DoC();
                            });
                        
                        R Offline
                        R Offline
                        reddipradeep.embd
                        wrote on 13 Feb 2020, 20:02 last edited by
                        #11

                        @presstab Hi, Sorry to bother you again, I have modified the code accordingly and getting the error in he below code " mainwindow.cpp:305:9: error: 'this' cannot be implicitly captured in this context "

                        QTimer::singleShot(122000, this, fRunC{
                        pJMPX->RDS_Set_11A_data(0x0008, 0x0000);
                        if (fRunC)
                        DoC();
                        });

                        J P 2 Replies Last reply 13 Feb 2020, 20:07
                        0
                        • R reddipradeep.embd
                          13 Feb 2020, 20:02

                          @presstab Hi, Sorry to bother you again, I have modified the code accordingly and getting the error in he below code " mainwindow.cpp:305:9: error: 'this' cannot be implicitly captured in this context "

                          QTimer::singleShot(122000, this, fRunC{
                          pJMPX->RDS_Set_11A_data(0x0008, 0x0000);
                          if (fRunC)
                          DoC();
                          });

                          J Offline
                          J Offline
                          JonB
                          wrote on 13 Feb 2020, 20:07 last edited by JonB
                          #12

                          @reddipradeep-embd

                          QTimer::singleShot(122000, this, fRunC{

                          Why do you use fRunC where the code to copy uses [fRunC]() ?

                          EDIT LOL, I just realised, I guess

                          [fRunC]()
                          

                          comes out looking like fRunC on this forum if you don't Code tags it :)

                          1 Reply Last reply
                          1
                          • R reddipradeep.embd
                            13 Feb 2020, 20:02

                            @presstab Hi, Sorry to bother you again, I have modified the code accordingly and getting the error in he below code " mainwindow.cpp:305:9: error: 'this' cannot be implicitly captured in this context "

                            QTimer::singleShot(122000, this, fRunC{
                            pJMPX->RDS_Set_11A_data(0x0008, 0x0000);
                            if (fRunC)
                            DoC();
                            });

                            P Offline
                            P Offline
                            presstab
                            wrote on 13 Feb 2020, 20:17 last edited by
                            #13

                            @reddipradeep-embd You are close, keep playing around with it until you get it to work.

                            1 Reply Last reply
                            0
                            • mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 13 Feb 2020, 20:54 last edited by
                              #14

                              Hi
                              What does the singleShot actually do ?

                              QTimer::singleShot(122000, this, this{
                              pJMPX->RDS_Set_11A_data(0x0008, 0x0000);
                              });

                              does it have to wait 12200 ms after calling
                              pJMPX->RDS_Set_11A_Enable(true);

                              or why this design ?

                              1 Reply Last reply
                              0

                              1/14

                              10 Feb 2020, 19:58

                              • Login

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