Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Adding Pushbuttons Dynamically
Forum Updated to NodeBB v4.3 + New Features

Adding Pushbuttons Dynamically

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
13 Posts 3 Posters 14.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DivyaMuthyala
    wrote on last edited by
    #3

    Hi Jsulm,

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

    jsulmJ 1 Reply Last reply
    0
    • D DivyaMuthyala

      Hi Jsulm,

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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #4

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DivyaMuthyala
        wrote on last edited by
        #5

        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.

        jsulmJ 1 Reply Last reply
        0
        • D DivyaMuthyala

          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.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #6

          @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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DivyaMuthyala
            wrote on last edited by
            #7

            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?

            jsulmJ 1 Reply Last reply
            0
            • D DivyaMuthyala

              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?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #8

              @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?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • D Offline
                D Offline
                DivyaMuthyala
                wrote on last edited by
                #9

                hi JSlum,

                How to retrieve data from database?

                jsulmJ 1 Reply Last reply
                0
                • D DivyaMuthyala

                  hi JSlum,

                  How to retrieve data from database?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

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

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • K Offline
                    K Offline
                    kumararajas
                    wrote on last edited by
                    #11

                    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.

                    --Kumar

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      kumararajas
                      wrote on last edited by
                      #12

                      @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

                      --Kumar

                      jsulmJ 1 Reply Last reply
                      0
                      • K kumararajas

                        @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

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #13

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

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        2

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved