[Solved] Force QScrollArea to add items from top down with equal spacing?
I've got some test code (below) to add items to a QScrollArea. I've worked out most of the issues but I don't know how to force the items to be placed progressively from the top down (stacked under each other). The QScrollArea is laid out in a .ui file and the rest is done through code. Have tried spacing, alignment etc. without success. Any tips?
Buttons seem to be added at equal divisions of the available space until there are enough items to force a scroll. The second image is how I want them spaced no matter how many items there are.
Called at start up
v = new QVBoxLayout;
// ui->myScroll->setContentsMargins(5,5,5,200); // v->setAlignment( ui->myScroll, Qt::AlignTop);//does nothing@
Method to add items
QPushButton *b = new QPushButton(this);
QHBoxLayout *h = new QHBoxLayout(); h->addWidget(b,0, Qt::AlignLeft); h->setAlignment(Qt::AlignTop); // h->setSpacing(0); // h->addStrut(12); // h->setContentsMargins(5,5,5,0); // h->addSpacing(112); v->addLayout(h); // QSpacerItem *item = new QSpacerItem(1,500, QSizePolicy::Expanding, QSizePolicy::Expanding); // v->addSpacerItem(item);
The spacing of the buttons is a function of the layout of the container widget (i.e. whatever "v" is set on), not the scroll area. I suggest you try v->setSizeConstraint(QLayout::SetFixedSize) and the layout should force the container widget to track the total vertical size hints of the contained widgets.
That works – thanks!