Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a container widget with a QVBoxLayout.

I have about 100 widgets added into the VBoxLayout. They all have the same height. At some point there's a need to resize one of the rows. I resize(newWidth, newHeight) the widget and call layout() on the QVBoxLayout, but nothing changes. the size remains fixed.

How do I dynamically change the height of widgets in QVBoxLayout?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

A resize of a widget that is inserted in a layout is a no-op, by design. The layout sizes the widgets, not you.

If you want a fixed-height widget in a QVBoxLayout, you have to set the widget's minimum and maximum height, and ensure its sizePolicy is not ignored in vertical direction.

If you want a widget that's twice the height of all other widgets. Set its stretch to 2, and stretch of other widgets to 1. Call layout->setStretch(index, stretch). The widgets will be assigned width proportionally to their stretches. For example, with all stretches at 1 and one stretch at 2, the widget with stretch 2 will get twice the room.

Everything else being equal (that's the important bit!), stretches are proportional to pixel sizes of widgets. If you want, say all widgets to be 20 pixels tall, but one to be 30 pixels tall, just set their stretches like so.

If the layout has the exact height so that the sum of all widget heights + spacings + margins adds up to layout height, you'll get height=stretch for widgets in the layout. Conversely, if you start with layout height, subtract the top and bottom margins and (widget count - 1)*spacing, you'll have height left for the widgets. You can then manually distribute that height among the widgets by setting stretch equal to desired height.

You may need to override the sizePolicy of the widgets, if they are too big by default.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...