PDA

View Full Version : More layout woes: Can a QLayout be made to respect its parent's dimensions?



hgiese
4th September 2009, 15:04
Hello out there,
I want to line up some widgets in a "row":
- flush left: a combo and a label
- flush right: 2 buttons
and a spacer in between to fill out the remaining space.

The goal: If the parent of the "line" shrinks or grows, the widgets on the left resp. right side should stay flush left (right) and the spacer in the middle should take care of the space in the middle. I thought that a horizontal layout would be what is needed but apparently I am wrong.
If you look at image 1 you see the widgets lined up in a frame. Note that the combo and the buttons have their original/default width reduced.
In image 2 I have a horizontal layout applied to them:
- The whole "line" now extends way beyond the parent's width, and
- the combo as well as the buttons returned to their default width.

2 questions please:
- What is the "Qt way" of achieving what I want: 2 widgets stay on the left, 2 on the right, their sizes do not change and the middle part takes care of the remaining space?
- Since I evidently do not use Qt's layouts the way they are intended to be used: Where can I learn how to use them?

Any help will be greatly appreciated.
Best regards
Helmut Giese

Boron
4th September 2009, 15:39
Give the parent widget a layout :).

hgiese
4th September 2009, 16:36
Hi Boron,
could you elaborate, please?
- If I start out like described above the horizontal layout is way too wide (and has ruined my resizing of the buttons).
- If I _then_ give the parent frame a vertical layout the frame adapts to the horizontal layout and gets as wide as the latter.
Ok, this is not the way to do it.

Ok, I start with an empty frame and give it a vertical layout first. No way, a layout can only be applied if there is at least one widget in it.
So I'm back at square one.
Now how can I make the parent determine the rules and not the horizontal layout?
And - as important - how do I make the horizontal layout respect my resizing of the buttons?
Any ideas?
Helmut Giese

Boron
4th September 2009, 17:41
A small exercise:
- Open a new form in Qt Designer, e.g. a dialog (with or without buttons; doesn't matter).
- Place Group Box in it
- Place a Text Edit in the Group Box
If you now resize the dialog, the two elements stay where they are. The dialog can even be made smaller then it would require to display GroupBox and LineEdit.

- Resize the dialog to show the two elements.
- Select the GroupBox and click on the vertical or horizontal layout on the tool bar. The GroupBox and the LineEdit instantly are resized. the LineEdit now optically fits perfect in the GroupBox.
- Resize the GroupBox by dragging of of its edges --> You see the LineEdit automatically be resized as well.

Now click outside the GroupBox but still inside the dialog.
- Select one of the layouts in the tool bar.
- Seen what happened?
- Resize the dialog --> The elements inside the form are resized as well.

That's what I meant with "Give the parent widget a layout".

Sometimes it not that easy to achieve the form to behave as you like. Very often it requires, beside the "layouts", also proper settings of the "sizePolicy" of the widgets in the properties window.

hgiese
5th September 2009, 15:24
Hi Boron,
thanks for the exercise - I think I understand Qt's layout engine a bit better now.
Best regards
Helmut Giese