Dynamically add buttons to ScrollArea



  • alt text

    So I've tried a fair amount of times to get this so when I pressed the + button(and after filling some information out, that it would add another button similar to the first, but any attempts I've seen and even copy pasted into mine have not done anything or caused things to just crash unexpectedly.

    How is this supposed to be done if these are just standard pushbuttons?

    (also tried to just move an existing button into the scrollarea with no success.)


  • Lifetime Qt Champion

    Hi,

    As a quick idea from your description:

    • a container widget that you put in the QScrollArea,
    • a QHBoxLayout on that widget.
    • Add your new buttons to that horizontal layout.

    What did you try ?



  • @SGaist I'm pretty much lost completely as to what I'm supposed to be doing. As of right now the setup is:

    • frame
      • scrollArea
        • scrollAreaContents
          • horizontalLayout
            • pushButton (x5)

    I don't know what parts I need to be writing code for. I had things scrolling fine at some point last night but kept messing with different code trying to get it all working and nothing works, so back to 0 code in source.



  • This post is deleted!

  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

    You can use insertWidget.



  • @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);
    


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



  • @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);
    


  • 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); 
                }
            }
    


  • @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;
      }
    }
    


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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.