tsao01
tsao01

Reputation: 101

How to identify the pressed button in Qt C++?

I have 4 buttons on my main window. Each button opens its own window with its own data. How to identify the pressed button to open right window? For example: I press sales button and it opens a window that shows information about ticket sales.

Mainwindow ui

Here is my code from mainwindow h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <sales.h>
#include <theatres.h>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    void button_pressed();

private:
    Ui::MainWindow *ui;
    sales *s;
    theatres *t;
};
#endif // MAINWINDOW_H

And here is my code from mainwindow cpp:

#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "build/sqlite/sqlite3.h"
#include <QtSql/QSqlDatabase>
#include <QTableView>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect((*ui).pushButton,SIGNAL(released()), this, SLOT(button_pressed()));
}

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

void MainWindow::button_pressed()
{
    
    s = new sales(this);
    s -> show();
}

Upvotes: 0

Views: 2427

Answers (4)

Allen Zhu
Allen Zhu

Reputation: 870

#include "widget.h"
#include "./ui_widget.h"

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

    connect(ui->btn_0,&QPushButton::clicked,this,&Widget::SlotButtonClicked);
    connect(ui->btn_1,&QPushButton::clicked,this,&Widget::SlotButtonClicked);
    connect(ui->btn_2,&QPushButton::clicked,this,&Widget::SlotButtonClicked);
    connect(ui->btn_3,&QPushButton::clicked,this,&Widget::SlotButtonClicked);
}

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

void Widget::SlotButtonClicked()
{
    auto sender = this->sender();
    if ( sender == ui->btn_0 ) {
        //  Click btn_0 to open widget0
    } else if ( sender == ui->btn_1 ) {
        //  Click btn_1 to open widget1
    } else if ( sender == ui->btn_2 ) {
        //  Click btn_2 to open widget2
    } else if ( sender == ui->btn_3 ) {
        //  Click btn_3 to open widget3
    }
}

Upvotes: 1

Andy Newman
Andy Newman

Reputation: 1208

The usual way to do this would be to connect the 4 different buttons to 4 different slots. It looks like you are using QtDesigner so that shouldn't be an issue.

If you were generating an array of buttons at run time you'd run into problems and would need a different solution. You could try something like this to pass an array index to the function, for example:

connect(button[x], &QPushButton::clicked, this, [this, x]() { button_pressed(x); });

Or you could do it the Qt way, which would be to call ::setProperty to store data in the button, and then retrieve it from the event, but it's so esoteric that I can't actually remember how to do that...

Upvotes: 0

JohnMcLaren
JohnMcLaren

Reputation: 31

As Andy Newman already answered the shortest solution is a lambda function

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QHBoxLayout>
#include <QDebug>

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

QHBoxLayout *h_layout = new QHBoxLayout;

centralWidget()->setLayout(h_layout);

    for(int c =1; c <= 10; c++)
    {
    QPushButton *button = new QPushButton(this); // create button

        button->setText(QString::number(c));  // set button id
        h_layout->addWidget(button);    // add a button to the form

        // lambda magic
        /* connecting a button signal to a lambda function that captures a pointer to a
             button and invokes an arbitrary type function. */
        connect(button, &QPushButton::clicked, [this, button]() {
            pressedButton(button->text());
        });
    }
}

void MainWindow::pressedButton(const QString &id_button)
{
    qDebug("Pressed button: %ls", id_button.utf16());
}

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

Upvotes: 1

joaopedro
joaopedro

Reputation: 75

If you can use Qt Designer, the best way to do this is to click with button right on the QPushButton (On .ui file in Qt Designer) and click to "Go to Slot", this will create a private slot to this button! In the header file will create the definition, like this:

private slots:

    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

    void on_pushButton_4_clicked();

And in the source file (.cpp) will create the "function" clicked pushButton:

void MainWindow::on_pushButton_clicked()
{

}

void MainWindow::on_pushButton_2_clicked()
{

}

void MainWindow::on_pushButton_3_clicked()
{

}

void MainWindow::on_pushButton_4_clicked()
{

}

Inside of the "function" in .cpp, you put the task that you want this button to do, in this case, to open a new window!

When you click "go to slot" in another button, will create another private slot with the respective number (If is the second QPushButton that you create, the private slot will be called by pushButton_2).

Upvotes: 0

Related Questions