PDA

View Full Version : Dynamically changing QGroupBox size



T4ng10r
29th March 2007, 20:09
In one of my windows I've placed QVBoxLayout with three QGroupBox inside. Each of QGroupBox posses his own QVBoxLayout in which resides custom widgets. QGroupBox are set without MaximumSize.
Now - if I click on custom widgets - the should show extra QLabel below. But those lines are ... painted vertically squeezed.
It looks like they are trying to be painted in place previously used only by one line. Hmm - maybe they need QSizePolicy? So for every QGroupBox I've set verticalPolicy to Expanding (so they should be shrinking and expanding when need).
Almost no change. Now when I click - for a second space is prepared - but then everything is collapsing to state before.

If I understand correctly - I need to say to QGroupBox in which one of item should expand to increase his size. This QGroupBox should say to Layout in which he resides - I need more space. But don't know how? I never use setMaximumHeight - only twice setMinimumHeight (for max height of QLabel). In one QGroupBox there are 8 items, in second - 4 and in last one only ONE. So place is enough to expand.

wysota
29th March 2007, 21:35
How do you show the label? Is it part of the custom widget or do you add it to the layout of the group box? Do your custom widgets have layouts or define sizeHints?

T4ng10r
30th March 2007, 05:50
Dialog is in one Layout. In this layout are 3 QGroupBox. Each groupbox have set internal Layout. In this layout resides custom widgets. This custom widgets are build with another internal Layout with two labels inside. One is always visible, other - only when user clicked in widget (by show/hide methods). Both labels have setMinimumHeight set to 25.

The only way I see is to do this by changing sizeHint or StretchFactor in SizePolicy each time user clicked.
Are there easier way?

Other question is - how sizes are prepared? Is Layout on highest level (one for Dialog) asking his members - give me how much space you need. Those members asking their own, and so on. And only way to split space by parts ( half for first, third part for second, rest for second) is by using SizePolicy?
If it's made that way - then only way I see here is by increasing vertical stretch factor every time when internal element are expanding and decrease - when they collapse.

wysota
30th March 2007, 11:29
The only way I see is to do this by changing sizeHint or StretchFactor in SizePolicy each time user clicked.
Are there easier way?
If you have a layout then you don't need to change the size hint. Stretch factor is also not the way to go. First thing I'd try is to call invalidate() on the layout of the custom widget that changes.


Other question is - how sizes are prepared? Is Layout on highest level (one for Dialog) asking his members - give me how much space you need. Those members asking their own, and so on. And only way to split space by parts ( half for first, third part for second, rest for second) is by using SizePolicy?
In general the top level widget asks its layout to position its items according to the top level widget size. Then the layout asks each of its items for their sizeHint. Then the real size is calculated based on the size hint, minimum and maximum sizes and size policies. Then widgets are repositioned and asked to adjust their layouts.

T4ng10r
30th March 2007, 13:20
In general the top level widget asks its layout to position its items according to the top level widget size. Then the layout asks each of its items for their sizeHint. Then the real size is calculated based on the size hint, minimum and maximum sizes and size policies. Then widgets are repositioned and asked to adjust their layouts.
For me it sounds that once top level widget set size of their layout items - their sizes won't change. If children placed in layout requires more space to be drawn correctly - there's no way to change size 'auto-magically'.
Once problem is only ONE LEVEL deep - it's ok.
Look below.

-------------DIALOG-----------------------------------
| -------------groupBox1--------------------------- |
| | item_1 | |
| | item_2 | |
| | - item_2_1 | |
| | item_3 | |
| | item_4 | |
| ------------------------------------------------- |
| -------------groupBox2--------------------------- |
| | item_1 | |
| | item_2 | |
| | ... | |
| ------------------------------------------------- |
| -------------groupBox3--------------------------- |
| | item_1 | |
| ------------------------------------------------- |
-----------------------------------------------------

Dialog Layout prepares space for all groupBoxes. If they are without sizeHint, minimum- , maximumSize and sizePolicies - size of all would be the same.
Now - as long no item_x_1 is showed - no problem. It looks ok.
When item_2_1 is show() - space for groupBox1 is not enough. groupBox1 inside layout tries to place all items as best as possible in given space. But its not enough.
Perfectly would be if - when item_2_1->show() layout gets info - I need more space. Then he send this info to layout()->parent(), which reorganizes his layout to give more space to this child.

In other place I used something like that - when user clicked inside groupBox - widget which was hidden is now showed. While processing click signal - I'm increasing this groupBox vertical stretch.
It works, but - in my opinion - result aren't ... elegant.
Modifying sizeHint, maximumSize and minimumSize when new child member is showed - with Layouts use - is nonsense and pointless.
Here I should transfer signal to Dialog, change stretchFactor and redraw - not nice and flexible enough.
Sounds -

wysota
30th March 2007, 15:02
I think you are wrong. See the attached example. The size constraint is not required, it just adjusts the top level widget size.