Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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



  • @reddipradeep-embd
    You have to show code.



  • 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 ) ?

    }


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


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



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


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





  • @fcarney

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


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



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



  • @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 :)



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


  • Lifetime Qt Champion

    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 ?


Log in to reply