PDA

View Full Version : widgets aren't resized correctly when adding them to layout



cptG
17th March 2010, 22:04
hello!
i have the following structure:
QWidget->QWidget->QBoxLayout->[QWidget1...QWidgetN]
where QWidget1 to QWidgetN are inserted one after another while the top-widgets are visible.
the problem is that while the widgets are added, the layout doesn't resize correctly, such that the widgets are way too small and their content is not visible.
the best way to see what i mean is by compiling the following (short) example:
http://homepages.physik.uni-muenchen.de/~thomas.gahr/resizeTestNew2.zip
the problem doesn't occur for widgets that are added before the main widget is visible... also, in the example QWidget1...QWidgetN are QWidgets containing a QHBoxLayout with a QLayout and a QDoubleSpinBox. When using JUST a QLabel, the problem doesn't occur...
how can i make the layout resize correctly to its contents? calling resize(sizeHint()) doesn't help, neither does calling updateGeometry and adjustSizes...

thanks,

thomas

ChrisW67
18th March 2010, 02:50
"container-widget" is a child of "top-widget" but not in a layout.

Set a layout on "top-widget" inside the scroll area, add "container-widget", and dispense with the attempts to force geometry in the addNewWidget() method. See if this helps.

cptG
18th March 2010, 08:23
unfortunately this is not a possibility since in top-widget there will be several container-widgets that are freely movable by the user...
but what would be the difference? in other words: what methods would a layout call on container-widget that makes it update the geometry correctly other than updateGeometry()?

ChrisW67
18th March 2010, 09:10
The layout inside your "container-widget" attempts to layout the specified children within the space it has available. It will not grow the container (which is what a layout of "top-widget" would do). The parent widget layout will (presumably) call setGeometry() and move() on its child widgets once it has worked out where to put them and what size they should be.

How do you intend the container widgets to be sized?

cptG
18th March 2010, 09:35
I see. The thing is: if the widgets that are added in addNewWidget() (let's call them items) are simply QLabel instead of the more complex widgets (i.e. having a layout and children in that layout), the container gets resized correctly, so i think (also according to your explanation) that the layouts of the items don't try to resize the item, such that their children don't occupy the space i would expect them to.
what i would expect:
once an item is added to the container widget, the latter grows such that all children occupy their preferred space (i.e. according to their sizeHint()?).
so thinking about it i guess my problem is not the container widget not growing to satisfy its children's sizeHints but rather the sizeHint of the items not being correct...
would it help to adjust the sizePolicy of all widgets inside an item (the QLabel and the QDoubleSpinBox in my example) to be minimum of minimumExpanding?

cptG
18th March 2010, 09:54
ha! i got it!
ok, first of all sorry for replying to my own post, i've been trying around as i was writing here on the forum...
setting the sizePolicy didn't do the trick, but setting the sizeConstraint to QLayout::SetMinimumSize on all layouts does exactly what i want. by all layouts i mean really all layouts: the layout of the container-widget, the layout of the groupBox inside the latter layout and the layout of the items.
maybe this can help someone who has the same problem in the future.
thanks for sticking around, ChrisW67!