6
votes

I would like to manage the space between my widgets, verticaly and horizontaly.

Here is a picture that illustrate what I want:

enter image description here

When I click on the red point, I want to reduce/extend the spaces where the red lines are (vertically).

When I click on the blue point, I want to reduce/extend the spaces where the blue lines are (horizontally).

My architecture is like that:

A main Widget with a layout, which contains the left/right arrow buttons and a QWidget for all the white views.

This QWidget contains a QGridLayout. In this QGridLayout I have the "left", "back"... widgets (in white on the screenshot).

  • QWidget with a layout
    • left arrow
    • right arrow
    • QWidget with a QGridLayout (GRIDLAYOUT)
      • QWidget with a VBoxLayout and 2 labels inside. (VIEW)

I tried to set the VIEW->setContentsMargins(); but the white rectangle stay the same, only the spacing inside this rectangle change (between the border and the "left" label for example (1 and 2 on the screen)).

I also tried to set the GRIDLAYOUT->setContentsMargins(); but this time only the the spacing of number 3 and 4 on the screen are changing.

Obviously there is the same behaviour for the horizontal resizing. (the blue marks on the screenshot.

The spacing doesn't allow to manage vertical and horizontal spacing separately...

I hope that you understand what I try to explain :o

How can I manage this ?

Thank you in advance,

1
trying using spacers in between each cell. (i.e. add a vertical/horizontal spacer in grid layout between each component you add) .. I am not sure if this will solve you issue .. but definitely worth a tryPratham
What is supposed to grow/shrink when you change the spacing, and what is fixed-size?Mat
Vertically, what is supposed to grow are the space where the red lines are in the screenshot, and the white rectangle is supposed to shrink.Slot

1 Answers

9
votes

For managing the space inbetween the white rects you can easily use

QGridLayout()::setHorizontalSpacing(int spacing);
QGridLayout()::setVerticalSpacing(int spacing);

This will set up the space between the white rects but not the space between white rect and border of upper widget. For these you will have to use (inherited from QLayout):

QGridLayout()::setContentsMargins ( int left, int top, int right, int bottom )