Solved Dynamically add buttons to ScrollArea
-
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)
- horizontalLayout
- scrollAreaContents
- scrollArea
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.
- frame
-
This post is deleted! -
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.
-
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 actionsvoid 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.