I'm using Qt5 on Windows7.
In my current app I try to display and remove a number of push-buttons.
widget = new ButtonWidget(ui->frame); // frame is a QScrollArea
connect(ui->addBtns, SIGNAL(clicked()), widget, SLOT(addButtons()));
connect(ui->deleteBtns, SIGNAL(clicked()), widget, SLOT(deleteButtons()));
And the ButtonWidget
class is here:
ButtonWidget::ButtonWidget(QWidget * parent) : QWidget(parent)
{
gridLayout = new QGridLayout(parent);
}
static QStringList texts{...}; // various content (see explanations below)
void ButtonWidget::addButtons()
{
for(auto i = 0; i < texts.size(); i++)
{
gridLayout->addWidget(new QPushButton(texts[i], this), i / 5, i % 5);
}
}
void ButtonWidget::deleteButtons()
{
while(gridLayout->count())
{
delete gridLayout->itemAt(0)->widget();
}
}
If I set QStringList texts{"1\nok"};
I get this:
...which is ugly (centered and expanded horizontally on all layout).
If I set QStringList texts{"1\nok",...,"24\nok"};
I get this:
...which is/seems quite ok.
And finally, if I set QStringList texts{"1\nok",...,"36\nok"};
I get this:
...which is very bad, garbled, etc.
So, the question: Is there any way to fix this, i.e. to disable somehow this "shrink-to-fit" on the layout? A default button size would be just fine.
I would like to take advantage of the vertical-scroll feature of the QScrollArea, not to stuff and cram all the buttons in the (limited) available space...