Łukasz Lew
Łukasz Lew

Reputation: 50338

How to prevent a label from causing the whole layout to resize

I have a following Qt code:

QVBoxLayout* box = new QVBoxLayout;
label = new QLabel(); // will be set later dynamically
box->addWidget (label);

Text in label will be set later. The problem is that when label resizes, it resizes QVBoxLayout, and it resizes other neighboring widgets. I don't want to make a label or layout fixed width. Because I want them to resize with a whole window.

Is it possible to tell a widget to take all the place that it has in a layout, but not more?

Upvotes: 1

Views: 1460

Answers (5)

Exa
Exa

Reputation: 4110

You can either set a maximumSize or have a look at sizePolicy.

Upvotes: 0

Andy M
Andy M

Reputation: 6065

Maybe you could have a look at the following methods :

Upvotes: 0

Patrice Bernassola
Patrice Bernassola

Reputation: 14446

Have you tried to modify the size policy of widgets? You can accomplish what you want with this.

Here are all size policies: QSizePolicy — enum QSizePolicy::Policy

Upvotes: 1

Robin
Robin

Reputation: 1688

If you want the label to fill the space of one line all the time, if the text is just one line, then the solution would simply be:

QVBoxLayout* box = new QVBoxLayout;  
label = new QLabel(" "); // The space will create a vertical space in the layout  
box->addWidget (label);

The "empty" label will now behave exactly as it will after you set the text.

Upvotes: 0

alisami
alisami

Reputation: 350

I think you need to set the stretch factor (setStrechFactor) for the main window you are placing the layout.

Upvotes: 0

Related Questions