Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Dynamically add buttons to ScrollArea
Forum Updated to NodeBB v4.3 + New Features

Dynamically add buttons to ScrollArea

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 8.7k 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.
  • A Offline
    A Offline
    andrewhopps
    wrote on last edited by andrewhopps
    #4
    This post is deleted!
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #5

      Like I wrote, create a container widget to put in the QScrollArea using QScrollArea::setWidget.

      Don't set any layout on QScrollArea, it's just wrong.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      0
      • SGaistS SGaist

        Like I wrote, create a container widget to put in the QScrollArea using QScrollArea::setWidget.

        Don't set any layout on QScrollArea, it's just wrong.

        A Offline
        A Offline
        andrewhopps
        wrote on last edited by andrewhopps
        #6

        @SGaist And is setting a layout on the container widget what I'm supposed to be doing I would assume as it now operates how I imagined? So if the container is a frame, how am I adding new buttons to that in a specific place?

        For example: I want to add the button to be the 3rd widget out of the 5 or so.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #7

          You can use insertWidget.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          A 1 Reply Last reply
          0
          • SGaistS SGaist

            You can use insertWidget.

            A Offline
            A Offline
            andrewhopps
            wrote on last edited by andrewhopps
            #8

            @SGaist Okay, I'm missing something here. The only way I'm getting things to just display properly is with simple

                ui->scrollArea_2->setWidget(ui->frame_2);
            

            Something keeps confusing me and just sends me further back into not understanding what is going on. I have my layout, and I set the layout on my container, then I added my widgets, followed by setting my scrollarea to display my container widget and the program crashes upon start. With segmentation fault at the first use of hlayout.

                QHBoxLayout *hlayout;
                ui->scrollArea_2->setWidgetResizable(true);
                ui->frame_2->setLayout(hlayout);
                hlayout->addWidget(ui->pushButton_14);
                hlayout->addWidget(ui->pushButton_16);
                hlayout->addWidget(ui->pushButton_17);
                hlayout->addWidget(ui->pushButton_18);
                hlayout->addWidget(ui->pushButton_19);
                ui->scrollArea_2->setWidget(ui->frame_2);
            
            1 Reply Last reply
            0
            • hskoglundH Offline
              hskoglundH Offline
              hskoglund
              wrote on last edited by
              #9

              Hi, instead of
              QHBoxLayout *hlayout;
              try
              QHBoxLayout *hlayout = new QHBoxLayout;

              A 1 Reply Last reply
              1
              • hskoglundH hskoglund

                Hi, instead of
                QHBoxLayout *hlayout;
                try
                QHBoxLayout *hlayout = new QHBoxLayout;

                A Offline
                A Offline
                andrewhopps
                wrote on last edited by andrewhopps
                #10

                @hskoglund said in Dynamically add buttons to ScrollArea:

                QHBoxLayout *hlayout = new QHBoxLayout;

                Edited:

                That stopped the crashing and the order of code was off, and after rearranging to the following, everything works as originally intended. Much thanks to you both.

                    QHBoxLayout *hlayout = new QHBoxLayout;
                    ui->scrollArea_2->setWidget(ui->frame_2);
                    ui->frame_2->setLayout(hlayout);
                    ui->scrollArea_2->setWidgetResizable(true);
                    hlayout->addWidget(ui->pushButton_14);
                    hlayout->addWidget(ui->pushButton_16);
                    hlayout->addWidget(ui->pushButton_18);
                    hlayout->addWidget(ui->pushButton_19);
                    hlayout->insertWidget(0, ui->pushButton_7);
                
                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andrewhopps
                  wrote on last edited by
                  #11

                  Well to expand on the previous issue, I've been able to create my buttons and such, but have no way of accessing their clicked functions. Been looking around and saw a few people saying QSignalMapper, so gave that a few tries and nothing functional. Multiple no such signals when the function I was trying to connect most definitely existed and contained just a qDebug saying it worked. I have my container populated with database items, and no clue where to be placing what within or outside of this function.

                          qry->prepare("SELECT type || ' ' || number FROM Car WHERE rowid >= 2");
                          if(qry->exec())
                          {
                              while(qry->next())
                              {
                              QString cName;
                              cName = qry->value(0).toString();
                              QPushButton *newCar = new QPushButton(cName);
                              newCar->setStyleSheet("background-color: rgb(240, 240, 240);"
                                                    "background-image: url(:/Resources/Images/Resources/Images/carButton.png);"
                                                    "background-repeat: no-repeat;"
                                                    "background-attachment: fixed;"
                                                    "background-position: center;");
                              newCar->setFixedSize(213, 108); 
                              newCar->setIconSize(QSize(200,100)); 
                              hlayout->addWidget(newCar); 
                              }
                          }
                  
                  J.HilkJ 1 Reply Last reply
                  0
                  • A andrewhopps

                    Well to expand on the previous issue, I've been able to create my buttons and such, but have no way of accessing their clicked functions. Been looking around and saw a few people saying QSignalMapper, so gave that a few tries and nothing functional. Multiple no such signals when the function I was trying to connect most definitely existed and contained just a qDebug saying it worked. I have my container populated with database items, and no clue where to be placing what within or outside of this function.

                            qry->prepare("SELECT type || ' ' || number FROM Car WHERE rowid >= 2");
                            if(qry->exec())
                            {
                                while(qry->next())
                                {
                                QString cName;
                                cName = qry->value(0).toString();
                                QPushButton *newCar = new QPushButton(cName);
                                newCar->setStyleSheet("background-color: rgb(240, 240, 240);"
                                                      "background-image: url(:/Resources/Images/Resources/Images/carButton.png);"
                                                      "background-repeat: no-repeat;"
                                                      "background-attachment: fixed;"
                                                      "background-position: center;");
                                newCar->setFixedSize(213, 108); 
                                newCar->setIconSize(QSize(200,100)); 
                                hlayout->addWidget(newCar); 
                                }
                            }
                    
                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by J.Hilk
                    #12

                    @andrewhopps
                    hi,

                    I would suggest using lambdas in your case, should makes things a bit easier

                    ...
                    int index = 0;
                    while(qry->next())
                    {
                                .....
                                QPushButton *newCar = new QPushButton(cName);
                                ....
                               connect(newCar, &QPushButton::clicked, newCar, [=]{newCarClicked(index});
                               index++;
                    }
                    

                    in your class you add now the function newCarClicked and handle the actions

                    void newCarClicked(int index{
                      switch(index){
                       case 0: break;
                       ....
                       default:break;
                      }
                    }
                    

                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    A 1 Reply Last reply
                    2
                    • J.HilkJ J.Hilk

                      @andrewhopps
                      hi,

                      I would suggest using lambdas in your case, should makes things a bit easier

                      ...
                      int index = 0;
                      while(qry->next())
                      {
                                  .....
                                  QPushButton *newCar = new QPushButton(cName);
                                  ....
                                 connect(newCar, &QPushButton::clicked, newCar, [=]{newCarClicked(index});
                                 index++;
                      }
                      

                      in your class you add now the function newCarClicked and handle the actions

                      void newCarClicked(int index{
                        switch(index){
                         case 0: break;
                         ....
                         default:break;
                        }
                      }
                      
                      A Offline
                      A Offline
                      andrewhopps
                      wrote on last edited by
                      #13

                      @J.Hilk Absolutely perfect! Much cleaner code than what I was attempting and trying to copy from. Thank you very much.

                      1 Reply Last reply
                      0

                      • Login

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