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

How to use dynamically-created buttons



  • hello everybody, in my Qt app I have a class that generates dynamically some buttons.
    this is the class that generates the pushbuttons: I used the method "add buttons( i, v) to generate a number of buttons, the method is used in the MainWindow class.

    #ifndef GENERATEBUTTONS_H
    #define GENERATEBUTTONS_H
    
    #include <QVBoxLayout>
    
    class GenerateButtons
    {
    public:
        GenerateButtons();
        static void addButtons(int i , QVBoxLayout V );
    };
    
    #endif // GENERATEBUTTONS_H
    
    
    #include "generatebuttons.h"
    #include <QPushButton>
    
    GenerateButtons::GenerateButtons()
    {
    
    }
    
    void GenerateButtons::addButtons(int i, QVBoxLayout V )
    {
        for(int j = 0; j < i; j++)
        {
            QPushButton *Button = new QPushButton();
            Button->setObjectName("Button" + QString::number(j));       
            V.addWidget(Button);
        }
    }
    

    main window class:

    #include "generatebuttons.h"
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
       GenerateButtons::addButtons(10 , Verticallayout );
    }
    

    what I want to do is: on each button click I can generate a debug message or Label in the same window.
    but I found difficulties because the buttons ore not created statically and can't create a slot for each button as we do with the QT designer, can someone have a proposition or a solution for this.
    thanks


  • Lifetime Qt Champion

    @Hamza-B said in How to use dynamically-created buttons:

    but I found difficulties because the buttons ore not created statically

    What difficulties exactly?

    QPushButton *button = new QPushButton(this);
    connect(button,...);
    

    You also can use same slot for all these buttons or use a lambda function as slot.



  • You can connect them in the function where you create them:

        for(int j = 0; j < i; j++)
        {
            QPushButton *Button = new QPushButton();
    
            connect(Button, &QPushButton::clicked, [=]() {
                qDebug() << "button" << j << "clicked";
            });
    
            Button->setObjectName("Button" + QString::number(j));       
            V.addWidget(Button);
        }
    


  • @Lurkki said in How to use dynamically-created buttons:

    connect

    I couldn't use "connect" inside the function, the class did not recognize it.
    is there some include that must be added to the class to use "connect"?


  • Moderators

    @Hamza-B
    connect is a static function of QObject. If your class is not derived from QObject, you can still use the explicitly static call

    e.g.

    QObject::connect(Button, &QPushButton::clicked, [=]() {
                qDebug() << "button" << j << "clicked";
            });
    
    

Log in to reply