PDA

View Full Version : Nested Layouts and sizePolicy



pagapov
19th October 2010, 23:35
Hi,

I'm trying to create a sort of indented list of checkboxes in the Designer. Here's how the result should look like:
5364

I'm using Horizontal Spacers to create actual indents, setting their sizeType to Fixed and sizeHint to whatever size I like. Then I group each spacer with its checkbox and enclose them in a QHBoxLayout. Then I apply a vertical layout to the whole widget. In the Designer it looks like this:
5365

Now, when I resize the widget, the checkboxes are no longer spaced evenly in vertical direction:
5366

My question: how can I control the vertical sizing behavior of nested QHBoxLayouts? There appears to be no such thing as a sizePolicy for the QHBoxLayout... I've tried several options of layoutSizeConstraint, but they don't seem to have any effect.

Thanks!

schnitzel
19th October 2010, 23:46
Put those two checkboxes into a single vertical layout... then try setting the layoutLeftMargin value

pagapov
19th October 2010, 23:52
Thanks, but that doesn't really solve the problem with resizing:
5367
Still, the items in the nested layout behave differently compared to the others.

schnitzel
22nd October 2010, 19:56
you're right, I got the same result when I tried it.

It seems such a simple problem. I'm sure there is a solution, hopefully one of the more experienced users can provide some insights.

Talei
22nd October 2010, 21:56
Try to mess around with the layoutStretch values for main layout.
Results::
5379 5380

schnitzel
22nd October 2010, 22:38
got it to work by adding horizontal spacers with sizeType 'Fixed'.

5381

5382

pagapov
23rd October 2010, 03:06
Thanks, Talei! layoutStretch of 1,0,0,0,0,0... seems to work. Do you have a clue why would layoutStretch make a difference? I thought it only affects expandable widgets...


got it to work by adding horizontal spacers with sizeType 'Fixed'.

Schnitzel,

What exactly have you done to make it work? My original spacers also had sizeType=Fixed.


Try to mess around with the layoutStretch values for main layout.
I have noticed that with layoutStretch, although nested layouts do not expand to infinity, the vertical spacing of checkboxes is still not EXACTLY the same: there is a little extra space around enclosed widgets:
5384

wysota
23rd October 2010, 09:04
Is this what you wanted?

If your style makes the indented boxes move to the right too much, change the horizontal size policy of the first indented box to MinimumExpanding.

genjix
23rd October 2010, 09:49
I would put them in their own vertical layout, then put a spacer on the left and add to a horizontal layout, and lastly add it all to one big vertical layout.

wysota
23rd October 2010, 12:05
Actually probably the best method is to just use QTreeWidget and remove all the decorations from the viewport so that only items with checkboxes are rendered.

Talei
23rd October 2010, 21:52
@pagapov
I assume that You want fluid / resizable layout that will arrange Your widgets vertically even, with the horizontal item indent.

I don't know why Your Ui bahave that way. I tested with stretch and difference is only 2px. Here is my test Ui: 5391
and PNGs (with "test markers")
http://www.imagebam.com/image/8a479a103526056
http://www.imagebam.com/image/6e962d103523909
http://www.imagebam.com/image/aa3425103523910

As for the clue, I don't have any :), the only reason AFAIK for this to happens is that QVBoxLayout arrange items by their height and QHBoxLayout don't return correct height for the items inside it (from brief look at the src it actually return sizeHint so probably I'm wrong on this one). So it treats QHBoxLayout as expendable horizontally and thus the bigger size for this layout. But as I said, it's only untested theory, please correct my if I'm wrong, or simply see implementation of the Q[H/V]BoxLayout.
I stumble upon similar behaviour with splitters and nested layouts so stretch fixed behaviour for me partially, there was still around 5-10px "free space" for particular splitter with layout.

schnitzel
24th October 2010, 01:32
Schnitzel,

What exactly have you done to make it work? My original spacers also had sizeType=Fixed.



I'm not sure, but here is the project.
Maybe it was just a matter of getting rid of all intermediate files.

pagapov
24th October 2010, 03:21
Actually probably the best method is to just use QTreeWidget and remove all the decorations from the viewport so that only items with checkboxes are rendered.

Thanks for the suggestion (I already decided to go that way, actually)!
But my question at this point is more theoretical than practical. I mean, I was wondering why the layout doesn't behave like I expected, and thought that there absolutely MUST be a good reason for this which I'm just unaware of. Because it's such a basic thing! At least, I'd like to know if this behavior is expected (and documented, hopefully) or it is a bug or some overlooked case.
It all gets much worse when I try to compose a non-trivial grid layout...
Hope to get more answers from experts :)

Added after 6 minutes:


I don't know why Your Ui bahave that way. I tested with stretch and difference is only 2px
The actual difference depends on the style: 6 px in Plastique vs. 3 px in Windows Vista:
5394
Not that much, but I don't want my UI be 2px precise :(

wysota
24th October 2010, 09:29
There are three things involved when calculating offsets between items - space distribution (according to size hint and size policies) margins and spacing. Now spacing has two subcases - a fixed spacing and a flexible spacing where the style decides that spacing between some two objects should be different than spacing between other two objects because the style guidelines say so (e.g. two radio buttons should have less spacing between them than a radio button and a line edit because probably those two buttons are somehow related to each other, etc.). Margins are quite simple - each layout either has some margin or not, if you put a layout in a layout you will notice that the contents of the internal layout are a bit off compared to the contents of the external layout - that's because of the margin. The last part - size hint and size policies (and also stretch and all the stuff related to it) are well documented so it should be easy to see where their influence is.

So what you observe is the sum of the three components of space calculation, you have to decide which one (or two or maybe all three) is decisive in your situation.