1
votes

I'm developing an app with a complex hierarchy of widgets and layouts, but in short it has a central widget with a formulary as upper widget and a QScrollArea as buttom widget (by means of a QVBoxLayout).

That QScrollArea represents a list (grid layout indeed) of QPushButtons which can contain a huge number of buttons (or not).

I want my app fits the following constraints:

  • Both (form and list) consume all available horizontal space, redistributing its contents to fill all horizontal space (nor SpaceItems neither contents margins).
  • Both must save as vertical space as possible, in order to make "lines" close to each other.

I've solve partially my problem making use of setSizeConstraint(QLayout::SetFixedSize) on the form, which shrinks it vertically, but also horizontally, causing that both, list and form, have different widths, wich doesn't look like very well.

How can I achieve that? I mean, how can specify something like grow horizontally to fill the widget but shrink vertically has much as possible?

2

2 Answers

0
votes

Add a spacer as the last item to the layout:

gridLayout->addItem(new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Expanding), lastrow, 0);
0
votes

I think this is what you want:

If you know how many columns you will have (and it doesn't change), insertStretch() in the last column (although it might give you the same effect as using a spacer).

int columnCount = gridLayout()->columnCount();
gridLayout->insertStretch( columnCount(), 1 ); // Default stretch for other

Note that this will resize your buttons to the size Qt thinks they should be unless you are explicitly changing their widths.