-3

I am beginner as the developer in QT, Here is the task for the button D, below is the code for the buttons A,B and C. Need to write code for the button D, if we press button D, it should execute the operations of A, B and C buttons ( one after one).

 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()) // Button D
 {
 // Code to execute operations of button A, B and C ( one after one ) ?
 }
 `````
  • 5
    Why can't you just create 3 methods for each of the actions of A, B & C, & then call them as required, and call all of them when D is pressed? – DeducibleSteak Feb 10 '20 at 21:47
  • 2
    I've seen a bunch of variants of this question today. You should all get together and gang up on this problem. – user4581301 Feb 10 '20 at 21:58

2 Answers2

1

I don't know where you got the code you are using, but it doesn't match the question you asked. I'm going to answer the question actually asked rather than try to fix the code. This is the way it is done in Qt.

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

public slots:
    void aSlot();
    void bSlot();
    void cSlot();
    void dSlot();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

And the source

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    connect(ui->pushButton,   &QPushButton::clicked, this, &MainWindow::aSlot);
    connect(ui->pushButton_2, &QPushButton::clicked, this, &MainWindow::bSlot);
    connect(ui->pushButton_3, &QPushButton::clicked, this, &MainWindow::cSlot);
    connect(ui->pushButton_4, &QPushButton::clicked, this, &MainWindow::dSlot);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::aSlot()
{
    ui->plainTextEdit->appendPlainText("aSlot() called\n");
}

void MainWindow::bSlot()
{
    ui->plainTextEdit->appendPlainText("bSlot() called\n");
}

void MainWindow::cSlot()
{
    ui->plainTextEdit->appendPlainText("cSlot() called\n");
}

void MainWindow::dSlot()
{
    aSlot();
    bSlot();
    cSlot();
}

screenshot of program running

user3450148
  • 851
  • 5
  • 13
  • Thanks for the example. I have used same logic, but when i press it executing the buttons A, B and C operations all at a time. My requirement, it should complete the A and then come to B and then come to C. Eg: If buttons A, B and C are linked with 2 min song each, If I press button D, then it should play A, after completing A then it should play B and then C. Could you please help me in writing logic for this requirement. – Pradeep Kumar Feb 12 '20 at 16:08
  • You simply need a variable in the class to keep track of which button has already been pressed – user3450148 Feb 13 '20 at 17:04
  • Can you please provide sample code for the above example. – Pradeep Kumar Feb 13 '20 at 17:44
0

You should have a look at QButtonGroup. This class can be registered with buttons and later have a single function buttonClicked be called when one is clicked. A version of this function takes an identifier you specified when adding the buttons, another takes the button object itself. It makes it easy to have many buttons be handled by a single class through slots.

jpo38
  • 20,821
  • 10
  • 70
  • 151