Adding Pushbuttons Dynamically



  • Hi,

    I want to add pushbuttons dynamically, button adding data will be retrieved from database, after retrieving we have to add buttons dynamically.

    Can anyone please give solution to add buttons dynamically?
    Thanks in advance


  • Moderators

    @DivyaMuthyala What exactly is the problem? You can create QPushButton instances as any other class instances in C++. You should ask more specific questions.

    void MyClass::MySlot()
    {
        for (int i = 0; i < 10; ++i) {
            QPushButton button = new QPushButton(this);
            button->setText(QString::number(i));
            connect(button, SIGNAL(clicked(bool)), this, SLOT(onClicked(bool)));
            layout()->addWidget(button); // Add the button to the layout
            button->show();
        }
    }
    


  • Hi Jsulm,

    Here you have mentioned slot ,actually where is slot definition??


  • Moderators

    @DivyaMuthyala "Here you have mentioned slot ,actually where is slot definition??" - in the widget class where you want to add these buttons? This is already definition - do you mean declaration? This is just an example - where to do it in your case depends on your requirements/design which I don't know.



  • Hi Jsulm,

    For sample I am trying following code, but only one button is creating with text 0.
    CODE:
    int main(int argc, char *argv[])
    {
    QApplication app(argc,argv);
    QWidget *wid=new QWidget();
    QVBoxLayout *layout=new QVBoxLayout();
    for (int i = 0; i < 10; ++i){
    QPushButton *button = new QPushButton();
    button->setText(QString::number(i));
    QObject::connect(button, SIGNAL(clicked(bool)), wid, SLOT(onClicked(bool)));
    layout->addWidget(button);
    wid->setLayout(layout); // Add the button to the layout
    wid->show();

        return app.exec();}
    

    }
    Please check it once and guide me.


  • Moderators

    @DivyaMuthyala Please start to fix your code by yourself - you are doing many things wrong:

    int main(int argc, char *argv[])
    {
        QApplication app(argc,argv);
        QWidget *wid=new QWidget();
        QVBoxLayout *layout=new QVBoxLayout();
        for (int i = 0; i < 10; ++i){
           QPushButton *button = new QPushButton();
            button->setText(QString::number(i));
            //QObject::connect(button, SIGNAL(clicked(bool)), wid, SLOT(onClicked(bool))); <-- THERE IS NO SUCH SLOT IN QWIDGET!
            layout->addWidget(button);
            button->show();
        }
        wid->setLayout(layout); // Add the layout to widget!
        wid->show();
        return app.exec();
    }
    

    I just spent time to try out the code above: it works.



  • Here in for loop you just keeping less than 10, so You are telling to create ten buttons here, if in the information to create how many buttons was present in database and we have to retrieve from databse and we have to create, how we can approach?


  • Moderators

    @DivyaMuthyala Please ask specific questions!
    If you retrieve the number of buttons to create from the database then just replace 10 with the number you get from the db.
    What exactly is the problem?



  • hi JSlum,

    How to retrieve data from database?


  • Moderators

    @DivyaMuthyala How about reading documentation http://doc.qt.io/qt-5/sql-connecting.html ?



  • Hi Divya,

    In my application, even I wanted to have dynamic buttons and this how I have achieved.

    But I have a configuration that says what will be the maximum number of buttons that can be shown.

    I create two dimensional array to contain pointer for each button.

        actionButtons = new QPushButton*[buttonList.size()];
    

    Then this creates the button and updates the pointers in the array created above.
    I use signal mapper to connect signals to slot.

        for( unsigned int i=0; i < buttonList.size(); ++i )
        {
            actionButtons[i] = new QPushButton( this );
            actionButtons[i]->setText( buttonName );
    
            connect( actionButtons[i]
                   , SIGNAL( clicked() )
                   , mapper
                   , SLOT( map() ) );
    
            mapper->setMapping( actionButtons[i]
                              , actionButtons[i] );
        }
    

    So, by signal mapper, each button is connected to it's own signal. Finally connect the mapper to a slot for function execution.

        connect( mapper
               , SIGNAL( mapped( QWidget* ) )
               , this
               , SLOT( actionButtonClick( QWidget* ) ) );
    

    Implement the slot -

    void ButtonBar::actionButtonClick( QWidget* btn )
    {
        QPushButton* pushButton = qobject_cast< CustomPushButton* > ( btn );
        // Do the job
    

    In my case, I did not want to create and delete multiple times, as the button may appear or disappear for every second. So, I just hide them and show based on the need.
    You may have a different use, but just as a performance improvement, I try not to do much job on creating and deleting a lot on the fly, since this is an embedded system stuff.



  • @jsulm Sorry, I did not try to override your answers. They were perfect and appropriate! I just wanted add some code if that can help @DivyaMuthyala


  • Moderators

    @kumararajas That is perfectly fine! No problem! I'm not the only one allowed to write here :-)


Log in to reply