Qt 设计师 |将 table 的列拉伸到最大

Qt Designer | Stretch column of table to fullest

我正在使用 Qt4 Designer 创建一个简单的 GUI。 我在我的 Dialog 中添加了一个 QTableWidget 。我的 table 中只需要一列,并且我希望此列能够自行拉伸到最大宽度。不过好像是固定宽度的。

我尝试从编辑器中将设置 属性 horizontalHeaderStretchLastSection 设置为 true,但没有帮助。 谁能告诉我应该编辑哪个 属性 ?

下面是截图。

以下是我正在使用的 QTableWidget 的其余属性。

  <widget class="QDialogButtonBox" name="buttonBox">
   <property name="geometry">
    <rect>
     <x>180</x>
     <y>460</y>
     <width>341</width>
     <height>32</height>
    </rect>
   </property>
   <property name="orientation">
    <enum>Qt::Horizontal</enum>
   </property>
   <property name="standardButtons">
    <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
   </property>
  </widget>
  <widget class="QTableWidget" name="tableWidget">
   <property name="geometry">
    <rect>
     <x>40</x>
     <y>70</y>
     <width>256</width>
     <height>192</height>
    </rect>
   </property>
   <property name="sizePolicy">
    <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
     <horstretch>0</horstretch>
     <verstretch>0</verstretch>
    </sizepolicy>
   </property>
   <property name="frameShadow">
    <enum>QFrame::Plain</enum>
   </property>
   <property name="midLineWidth">
    <number>1</number>
   </property>
   <attribute name="horizontalHeaderVisible">
    <bool>false</bool>
   </attribute>
   <attribute name="horizontalHeaderDefaultSectionSize">
    <number>107</number>
   </attribute>
   <attribute name="horizontalHeaderHighlightSections">
    <bool>true</bool>
   </attribute>
   <attribute name="horizontalHeaderStretchLastSection">
    <bool>true</bool>
   </attribute>
   <attribute name="verticalHeaderVisible">
    <bool>true</bool>
   </attribute>
   <attribute name="verticalHeaderShowSortIndicator" stdset="0">
    <bool>false</bool>
   </attribute>
   <attribute name="verticalHeaderStretchLastSection">
    <bool>false</bool>
   </attribute>
   <row>
    <property name="text">
     <string>New Row</string>
    </property>
   </row>
   <row>
    <property name="text">
     <string>rrr</string>
    </property>
   </row>
   <row>
    <property name="text">
     <string>gggg</string>
    </property>
   </row>
  </widget>

您将垂直 header 与第一列混淆了。如果您想要 table 只有一列填满整个宽度,请隐藏垂直 header 并拉伸水平 header:

的最后一列

然后您应该编辑 table 项,添加一列(例如,称为 "First Column")和您需要的任意行(无论您如何称呼它们)。这将启用 Items 选项卡,您可以在其中添加实际的 table 内容。完成后,您的 table 应该如下所示:

我认为这里值得指出的是,没有 header 的 one-column table 或多或少等同于 ListWidget。因此,鉴于 ListWidget 具有更简单的 API,也许您应该考虑使用它。