can't resize widgets in QVBoxLayout

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?

Answers


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.


Need Your Help

Windows incremental backup script

windows backup scheduled-tasks

I'm from a linux background, and need to run an incremental backup script on windows. I already have a batch script which dumps my database into a file. What I'd like is to only keep backups for th...

SQL procedure takes very long time?

sql-server sql-server-2012

How to optimized below sql server procedure, tblPggg table has only 5000 records, and rest tables have only 50,100,200,10, records. Even it takes 15/20 secs on local on live server as well,