0
votes

I have an QHBoxLayout that includes two QLabel widgets. My left QLabel is much larger than my right QLabel, however, the QHBoxLayout is splitting the output in half, so the left side of the layout is too small and the right side of the layout is too big. How do I modify the QHBoxLayout to create unequal proportioned space for each included widget?

1

1 Answers

8
votes

The layout system should reserve more space for the larger label, if it needs it, and if there is space available.

If you want to force the layout to reserve specific amount of space for the larger label, you can use QBoxLayout::setStretch.

For example, if you want to reserve 70% of the space for the larger label, and 30% for the smaller one, you can use this:

ui->horizontalLayout->setStretch(0, 7);
ui->horizontalLayout->setStretch(1, 3);

Or, you can make the smaller label only reserve the absolute minimum space it needs, and the larger label to reserve as much space as it can by using size policies.

For the smaller label, set the horizontal size policy to QSizePolicy::Maximum, and for the larger label, set the horizontal size policy to QSizePolicy::Minimum (or QSizePolicy::MinimumExpanding).

ui->label_Large->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
ui->label_Small->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);

You can see what different size policies do here.