0
votes

The only post similar to this I could find is https://forum.qt.io/topic/62467/2-horizontal-layouts-1-with-fixed-height - but unfortunately, it does not answer what I want to ask here.

This is the layout I got to:

QtDesigner.png

So, I have a main QVBoxLayout, inside it I have two QHBoxLayout. I want the top QHBoxLayout to contain a single label that will stretch horizontally, but have a fixed height of 64 pixels. And from the linked post:

So with layouts, u set constraints on the widgets, not on the layouts.

That is what I had done in the screen shot above, however:

  • The top QHBoxLayout still takes up half of the window (judging by the red border), and the label is centered vertically in it.

What I want is: the label should be aligned to top of the window - and the layout should "wrap" around it, so I end up with a red layout border on top that is 64 pixels (plus whatever padding/margins may be there).

Is this possible to do in QtDesigner, and if so - how?

2
Have you tried to add a vertical spacer into the bottom cell?Ilya
Many thanks, @Ilya - just tried, and if I set both minimumSize and maximumSize Heights of the label to 64, it works fine! Will the vertical spacer in the bottom cell interfere with the widgets I'll want to place inside later? (PS. Feel free to post this as an answer, I'll accept it!)sdbbs
You are welcome! You can make width of this vertical spacer very small, so it will not break your layour. The only goal here is to make height of this bottom cell as big as it is possible.Ilya

2 Answers

1
votes

To guarantee that the top cell is not too big, you need to make the bottom one is expandable. To do this it is enough to add a vertical spacer. Something like this:

<layout ...>
 <item row="1" column="0">
  <spacer name="verticalSpacer">
   <property name="orientation">
    <enum>Qt::Vertical</enum>
   </property>
   <property name="sizeType">
    <enum>QSizePolicy::Expanding</enum>
   </property>
   <property name="sizeHint" stdset="0">
    <size>
     <width>...</width>
     <height>...</height>
    </size>
   </property>
  </spacer>
 </item>

It is obvious that you plan to add other items into the bottom cell. If at least one of them is vertically expandable, you don't need that vertical spacer.

2
votes

You can tell Boxlayouts how they should distribute space to the single columns/rows in the layoutStretch property. It is a ,-separated list of strech-factors. Just put 0,1 there, to make the top layout not stretch, and the bottom to take all the available space:

enter image description here