How to get the text of dynamically added QPushButton when clicked

306 views Asked by At

I have added a set of pushbuttons inside a QScrollarea dynamically , but I want to get the text of that particular button either by click or touch.

button = new QPushButton("someText");
button->setParent(ui->scrollArea);
button->setCheckable(true);
button->show();

I used button->text(); and button->currentText();. But it gives the text of the last added pushbutton, but not the clicked button.

3

There are 3 answers

0
A.R.M On BEST ANSWER

G.M. said:

For what it's worth it sounds like you might want to use a lambda as the slot when you call connect.

One way to get any QPushButton's text when clicked, is to connect its clicked() slot to a lambda that stores button text, in a QString for example:

QPushButton button = new QPushButton();
button->setText("some text");
QString buttonText;

QObject::connect(button,&QPushButton::clicked,[=](){ buttonText = button->text(); qDebug()<<buttonText; });

qDebug is just there to confirm that the button's text has been stored in buttonText, for debugging purposes.

1
Mike On

Ok, it's not perfect and there will be a better way I'm sure but this adds four buttons to a vertical layout within a Scroll Area, when you click on each button it will display the Push Button text in a Debug Window, this can be adapted to send it to wherever you wish.

Mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class QPushButton;

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

public slots:
    void handleClick1();
    void handleClick2();
    void handleClick3();
    void handleClick4();

private:
    Ui::MainWindow *ui;
    QPushButton *_pbTest1;
    QPushButton *_pbTest2;
    QPushButton *_pbTest3;
    QPushButton *_pbTest4;
};
#endif // MAINWINDOW_H

Implementation (MainWindow.cpp):

#include <QPushButton>
#include <QHBoxLayout>
#include <QDebug>

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

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , _pbTest1(new QPushButton)
    , _pbTest2(new QPushButton)
    , _pbTest3(new QPushButton)
    , _pbTest4(new QPushButton)
{
    ui->setupUi(this);
    _pbTest1->setText("Well I'm...");
    _pbTest2->setText("Back In Black...");
    _pbTest3->setText("I hit the sack...");
    _pbTest4->setText("It's good to be back...");

    auto layout = new QVBoxLayout(ui->scrollArea);

    layout->addWidget(_pbTest1);
    layout->addWidget(_pbTest2);
    layout->addWidget(_pbTest3);
    layout->addWidget(_pbTest4);

    connect(_pbTest1, &QPushButton::clicked, this, &MainWindow::handleClick1);
    connect(_pbTest2, &QPushButton::clicked, this, &MainWindow::handleClick2);
    connect(_pbTest3, &QPushButton::clicked, this, &MainWindow::handleClick3);
    connect(_pbTest4, &QPushButton::clicked, this, &MainWindow::handleClick4);
}

void MainWindow::handleClick1() {
    qDebug()<<_pbTest1->text();
}
void MainWindow::handleClick2() {
    qDebug()<<_pbTest2->text();
}

void MainWindow::handleClick3() {
    qDebug()<<_pbTest3->text();
}

void MainWindow::handleClick4() {
    qDebug()<<_pbTest4->text();
}


MainWindow::~MainWindow()
{
    delete _pbTest1;
    delete _pbTest2;
    delete _pbTest3;
    delete _pbTest4;
    delete ui;
}

Main.cpp

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

Outputs the following to a Debug window:

14:54:41: Starting /home/qt-book/build-PushButtonText-Desktop_Qt_5_12_12_GCC_64bit-Debug/PushButtonText...
"Well I'm..."
"Back In Black..."
"I hit the sack..."
"It's good to be back..."
14:54:46: /home/qt-book/build-PushButtonText-Desktop_Qt_5_12_12_GCC_64bit-Debug/PushButtonText exited with code 0

Runs on Ubuntu 20.04, QT 5.12.12.

1
Neocoder_1 On
button = new QPushButton();
button->setText(str); //str is text from database

globalButtonName = str;

button->move(buttonPos);
button->setCheckable(true);
button->show();

QObject::connect(button, &QPushButton::clicked, [=]() { globalButtonName = buttonName;});