1
votes

I made a form that looks like this: screenshot of the form. The .ui is below.

The QTextEdit on the right is the only widget that has it's horizontal size policy set to Expanding. All the other widgets are either Preferred or Minimum. Yet when I resize the window horizontally, the text edit is the only widget that does not grow. It stays the same size while all the other widgets expand horizontally.

I guess there's something that I don't understand about how all of that works. What do I have to do to make the text edit expand when I resize the window?

.ui file

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWidget</class>
 <widget class="QWidget" name="MainWidget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>634</width>
    <height>303</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QGridLayout" name="gridLayout">
   <item row="3" column="4" rowspan="7" colspan="2">
    <widget class="QTextEdit" name="textEdit">
     <property name="sizePolicy">
      <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
       <horstretch>0</horstretch>
       <verstretch>0</verstretch>
      </sizepolicy>
     </property>
     <property name="minimumSize">
      <size>
       <width>300</width>
       <height>0</height>
      </size>
     </property>
     <property name="readOnly">
      <bool>true</bool>
     </property>
    </widget>
   </item>
   <item row="5" column="2">
    <widget class="QLabel" name="label_5">
     <property name="text">
      <string>Data written:</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
     </property>
    </widget>
   </item>
   <item row="6" column="1">
    <widget class="QLabel" name="readSpeedLabel">
     <property name="frameShape">
      <enum>QFrame::StyledPanel</enum>
     </property>
     <property name="frameShadow">
      <enum>QFrame::Sunken</enum>
     </property>
     <property name="text">
      <string>TextLabel</string>
     </property>
    </widget>
   </item>
   <item row="5" column="0">
    <widget class="QLabel" name="label_3">
     <property name="text">
      <string>Write speed:</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
     </property>
    </widget>
   </item>
   <item row="4" column="0">
    <widget class="QLabel" name="label_2">
     <property name="text">
      <string>Test Mode:</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
     </property>
    </widget>
   </item>
   <item row="5" column="3">
    <widget class="QLabel" name="dataWrittenLabel">
     <property name="frameShape">
      <enum>QFrame::StyledPanel</enum>
     </property>
     <property name="frameShadow">
      <enum>QFrame::Sunken</enum>
     </property>
     <property name="text">
      <string>TextLabel</string>
     </property>
    </widget>
   </item>
   <item row="6" column="0">
    <widget class="QLabel" name="label_7">
     <property name="text">
      <string>Read speed:</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
     </property>
    </widget>
   </item>
   <item row="4" column="1" colspan="3">
    <widget class="QPushButton" name="testModePushButton">
     <property name="text">
      <string>Select testing mode</string>
     </property>
    </widget>
   </item>
   <item row="9" column="2" colspan="2">
    <widget class="QPushButton" name="startStopButton">
     <property name="enabled">
      <bool>false</bool>
     </property>
     <property name="text">
      <string>Start test</string>
     </property>
    </widget>
   </item>
   <item row="3" column="1" colspan="3">
    <widget class="QPushButton" name="devicePushButton">
     <property name="text">
      <string>Select device</string>
     </property>
    </widget>
   </item>
   <item row="7" column="3">
    <widget class="QLabel" name="ETALabel">
     <property name="frameShape">
      <enum>QFrame::StyledPanel</enum>
     </property>
     <property name="frameShadow">
      <enum>QFrame::Sunken</enum>
     </property>
     <property name="text">
      <string>TextLabel</string>
     </property>
    </widget>
   </item>
   <item row="5" column="1">
    <widget class="QLabel" name="writeSpeedLabel">
     <property name="frameShape">
      <enum>QFrame::StyledPanel</enum>
     </property>
     <property name="frameShadow">
      <enum>QFrame::Sunken</enum>
     </property>
     <property name="text">
      <string>TextLabel</string>
     </property>
    </widget>
   </item>
   <item row="7" column="2">
    <widget class="QLabel" name="label_11">
     <property name="text">
      <string>ETA:</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
     </property>
    </widget>
   </item>
   <item row="6" column="3">
    <widget class="QLabel" name="dataReadLabel">
     <property name="frameShape">
      <enum>QFrame::StyledPanel</enum>
     </property>
     <property name="frameShadow">
      <enum>QFrame::Sunken</enum>
     </property>
     <property name="text">
      <string>TextLabel</string>
     </property>
    </widget>
   </item>
   <item row="6" column="2">
    <widget class="QLabel" name="label_9">
     <property name="text">
      <string>Data Read:</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
     </property>
    </widget>
   </item>
   <item row="8" column="0" colspan="4">
    <spacer name="verticalSpacer">
     <property name="orientation">
      <enum>Qt::Vertical</enum>
     </property>
     <property name="sizeHint" stdset="0">
      <size>
       <width>20</width>
       <height>40</height>
      </size>
     </property>
    </spacer>
   </item>
   <item row="3" column="0">
    <widget class="QLabel" name="label">
     <property name="text">
      <string>Device:</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>
1

1 Answers

1
votes

It is the QLabel items which are messing your layout.

Set the horizontal size policy to Fixed for every QLabel and it works.


Now the answer to why QLabels with Preferred are growing faster than the QTextEdit with an Expanding policy, I do not know. Especially when changing Preferred to Fix solves the problem.

Qt documentation states :

QSizePolicy::Expanding The widget can make use of extra space, so it should get as much space as possible.

QSizePolicy::Preferred The widget can be expanded, but there is no advantage to it being larger than sizeHint().

QSizePolicy::Fixed The QWidget::sizeHint() is the only acceptable alternative, so the widget can never grow or shrink.

Looking at this, it seems that a widget with Preferred would not grow bigger than sizeHint() when next to another widget with Expanding. But in your case labels behave like they have the priority over the text edit. Note that this does not seem to be the case for QPushButton.

It looks like a bug to me.