mimic
mimic

Reputation: 5224

How to make a Qt widget change its size?

My problem probably is very simple, but I have no idea how to solve it.

So, I have several widgets in vertical layout. Some of them in some moment should be invisible. I suppose this moment the control that has expanding vertical policy should increase its height but it never happens. Why? How to force it to change the size? So far there is only one way to do that - to change the size of window manually a little bit and only after it the widget changes its height.

Upvotes: 4

Views: 4574

Answers (3)

Exa
Exa

Reputation: 4110

Have you tried setting the stretch on 1 for the widget you want to be expanded when others are hidden?

QBoxLayout::addWidget( my_widget, 1 );

When this widget is added with a stretch of 1 and the other widgets without stretch, this widget will expand to the available space. Maybe that will do the trick.

Upvotes: 0

TonyK
TonyK

Reputation: 17114

Try calling adjustSize() on the parent of the layout.

Upvotes: 1

Judge Maygarden
Judge Maygarden

Reputation: 27573

When you make a widget invisible (i.e. call hide), also remove it from the layout (see removeWidget). That way all of the other widgets will automatically resize to make use of the extra space. Since you are only hiding the widget, its space is still reserved in the vertical layout.

Make use of insertWidget to maintain the layout ordering when transitioning a hidden widget back into view. Otherwise, addWidget would always place it at the bottom. This approach should be less complex than managing the sizes manually.

Upvotes: 3

Related Questions