How to use two or three button's logic in single button using CPP in QT
-
wrote on 10 Feb 2020, 19:58 last edited by
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
-
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
wrote on 10 Feb 2020, 20:01 last edited by@reddipradeep-embd
You have to show code. -
wrote on 10 Feb 2020, 20:13 last edited by
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 ) ?
}
-
wrote on 11 Feb 2020, 01:52 last edited by
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(); } }
-
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.
wrote on 11 Feb 2020, 20:21 last edited by@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); }
-
@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); }
wrote on 12 Feb 2020, 21:25 last edited by@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
-
wrote on 12 Feb 2020, 21:56 last edited by
@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
-
@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
-
QTimer::singleShot(122000, this, [fRunC](){ pJMPX->RDS_Set_11A_data(0x0008, 0x0000); if (fRunC) DoC(); });
-
QTimer::singleShot(122000, this, [fRunC](){ pJMPX->RDS_Set_11A_data(0x0008, 0x0000); if (fRunC) DoC(); });
wrote on 13 Feb 2020, 20:02 last edited by@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();
}); -
@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();
});wrote on 13 Feb 2020, 20:07 last edited by JonB@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 :) -
@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();
});wrote on 13 Feb 2020, 20:17 last edited by@reddipradeep-embd You are close, keep playing around with it until you get it to work.
-
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/14