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
Qt 6.11 is out! See what's new in the release blog

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.8k 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.
  • presstabP presstab

    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();
    
    }
    
    }
    
    
    fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on 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.

    presstabP 1 Reply Last reply
    0
    • fcarneyF fcarney

      @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.

      presstabP Offline
      presstabP Offline
      presstab
      wrote on 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
      0
      • presstabP presstab

        @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 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
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on 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.

          presstabP 1 Reply Last reply
          1
          • fcarneyF fcarney

            @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

            presstabP Offline
            presstabP Offline
            presstab
            wrote on last edited by
            #9

            @fcarney

              QTimer::singleShot(122000, this, [fRunC](){
                    pJMPX->RDS_Set_11A_data(0x0008, 0x0000);
                    if (fRunC)
                        DoC();
                });
            
            fcarneyF R 2 Replies Last reply
            0
            • presstabP presstab

              @fcarney

                QTimer::singleShot(122000, this, [fRunC](){
                      pJMPX->RDS_Set_11A_data(0x0008, 0x0000);
                      if (fRunC)
                          DoC();
                  });
              
              fcarneyF Offline
              fcarneyF Offline
              fcarney
              wrote on 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
              • presstabP presstab

                @fcarney

                  QTimer::singleShot(122000, this, [fRunC](){
                        pJMPX->RDS_Set_11A_data(0x0008, 0x0000);
                        if (fRunC)
                            DoC();
                    });
                
                R Offline
                R Offline
                reddipradeep.embd
                wrote on 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();
                });

                JonBJ presstabP 2 Replies Last reply
                0
                • R reddipradeep.embd

                  @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();
                  });

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on 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

                    @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();
                    });

                    presstabP Offline
                    presstabP Offline
                    presstab
                    wrote on 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 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

                      • Login

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