Al C
Al C

Reputation: 5377

How can I anchor widgets in Qt Creator?

I have a dialog with a ListWidget on the left and a various widgets (including a TextEdit and LineEdit) inside a VBoxLayout to the right. There's a splitter between the ListWidget and VBox. Here's the UI code (minus a few property definitions):

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>701</width>
    <height>586</height>
   </rect>
  </property>
  <widget class="QSplitter" name="splitter">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>12</y>
     <width>681</width>
     <height>561</height>
    </rect>
   </property>
   <property name="orientation">
    <enum>Qt::Horizontal</enum>
   </property>
   <widget class="QListWidget" name="keysListWidget">
   </widget>
   <widget class="QWidget" name="">
    <layout class="QVBoxLayout" name="verticalLayout">
     <item>
      <layout class="QHBoxLayout" name="horizontalLayout">
       <item>
        <spacer name="horizontalSpacer">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
        </spacer>
       </item>
       <item>
        <widget class="QPushButton" name="deleteButton">
         <property name="text">
          <string>Delete</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QPushButton" name="saveNewButton">
         <property name="text">
          <string>Save New</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QPushButton" name="saveChangesButton">
         <property name="text">
          <string>Update</string>
         </property>
        </widget>
       </item>
      </layout>
     </item>
     <item>
      <widget class="QLineEdit" name="keyLineEdit">
      </widget>
     </item>
     <item>
      <widget class="QPlainTextEdit" name="valueTextEdit">
      </widget>
     </item>
    </layout>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

If a user stretches the dialog to the right, I'd like the VBox to remain anchored to the right side of the dialog (while the ListWidget's size remains unchanged). And if the user stretches the dialog down, I'd like the ListWidget and VBox to both remain anchored to the top and bottom of the dialog, i.e., I want them both to stretch.

Right now, stretching the dialog right or down has no effect on widget size.

Upvotes: 5

Views: 9685

Answers (1)

Pavel Strakhov
Pavel Strakhov

Reputation: 40492

You need to add a layout to the top widget. You need to right-click the most outside widget (Dialog), select "Lay out" and select appropriate layout (grid layout will do fine). This will ensure that direct children of the Dialog will react to its size changes.

To prevent stretching ListWidget horizontally you can set its maximum width (maximumSize property in Designer).

Upvotes: 8

Related Questions