0

我有一个 QScrollArea,其中有许多 QTreeWidgets 排列成 QVBoxLayout。每个树小部件都有一个顶级项目(但子项目的层次结构是任意的)。单个树小部件的垂直滚动条通过 禁用setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff)。我希望树小部件的垂直大小在它们的项目展开或折叠时动态改变,所以我setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents)为每个 QTreeWidgets 设置了。垂直大小策略设置为最小,并且在所有树小部件下方都有一个垂直间隔。布局间距设置为 0。现在,它像预期的那样工作,除了一件事:当树小部件完全折叠时,它仍然占用太多垂直空间,如图所示。我只是不知道如何摆脱这个额外的空间,以便树小部件在折叠时彼此相邻。

在此处输入图像描述.

Qt Designer中生成的ui-form的内容:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>241</width>
    <height>526</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <property name="spacing">
     <number>0</number>
    </property>
    <property name="sizeConstraint">
     <enum>QLayout::SetMinimumSize</enum>
    </property>
    <property name="leftMargin">
     <number>0</number>
    </property>
    <property name="topMargin">
     <number>0</number>
    </property>
    <property name="rightMargin">
     <number>0</number>
    </property>
    <property name="bottomMargin">
     <number>0</number>
    </property>
    <item>
     <widget class="QScrollArea" name="scrollArea">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Minimum" vsizetype="Minimum">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
      <property name="styleSheet">
       <string notr="true">background-color: rgb(255, 255, 255);</string>
      </property>
      <property name="frameShape">
       <enum>QFrame::NoFrame</enum>
      </property>
      <property name="lineWidth">
       <number>0</number>
      </property>
      <property name="widgetResizable">
       <bool>true</bool>
      </property>
      <widget class="QWidget" name="scrollAreaWidgetContents">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>241</width>
         <height>480</height>
        </rect>
       </property>
       <layout class="QVBoxLayout" name="verticalLayout_2">
        <property name="spacing">
         <number>10</number>
        </property>
        <property name="leftMargin">
         <number>0</number>
        </property>
        <property name="topMargin">
         <number>0</number>
        </property>
        <property name="rightMargin">
         <number>0</number>
        </property>
        <property name="bottomMargin">
         <number>0</number>
        </property>
        <item>
         <widget class="QTreeWidget" name="treeWidget">
          <property name="sizePolicy">
           <sizepolicy hsizetype="Expanding" vsizetype="Minimum">
            <horstretch>0</horstretch>
            <verstretch>0</verstretch>
           </sizepolicy>
          </property>
          <property name="frameShape">
           <enum>QFrame::NoFrame</enum>
          </property>
          <property name="lineWidth">
           <number>0</number>
          </property>
          <property name="verticalScrollBarPolicy">
           <enum>Qt::ScrollBarAlwaysOff</enum>
          </property>
          <property name="horizontalScrollBarPolicy">
           <enum>Qt::ScrollBarAlwaysOff</enum>
          </property>
          <property name="sizeAdjustPolicy">
           <enum>QAbstractScrollArea::AdjustToContents</enum>
          </property>
          <property name="alternatingRowColors">
           <bool>false</bool>
          </property>
          <property name="uniformRowHeights">
           <bool>false</bool>
          </property>
          <property name="animated">
           <bool>false</bool>
          </property>
          <property name="headerHidden">
           <bool>true</bool>
          </property>
          <attribute name="headerVisible">
           <bool>false</bool>
          </attribute>
          <column>
           <property name="text">
            <string>New Column</string>
           </property>
          </column>
          <item>
           <property name="text">
            <string>TreeWidget1</string>
           </property>
           <item>
            <property name="text">
             <string>New Item</string>
            </property>
           </item>
           <item>
            <property name="text">
             <string>New Item</string>
            </property>
           </item>
           <item>
            <property name="text">
             <string>New Item</string>
            </property>
           </item>
           <item>
            <property name="text">
             <string>New Item</string>
            </property>
           </item>
           <item>
            <property name="text">
             <string>New Item</string>
            </property>
            <item>
             <property name="text">
              <string>New Item</string>
             </property>
            </item>
            <item>
             <property name="text">
              <string>New Item</string>
             </property>
            </item>
            <item>
             <property name="text">
              <string>New Item</string>
             </property>
            </item>
            <item>
             <property name="text">
              <string>New Item</string>
             </property>
            </item>
           </item>
          </item>
         </widget>
        </item>
        <item>
         <widget class="QTreeWidget" name="treeWidget_2">
          <property name="sizePolicy">
           <sizepolicy hsizetype="Expanding" vsizetype="Minimum">
            <horstretch>0</horstretch>
            <verstretch>0</verstretch>
           </sizepolicy>
          </property>
          <property name="frameShape">
           <enum>QFrame::NoFrame</enum>
          </property>
          <property name="lineWidth">
           <number>0</number>
          </property>
          <property name="verticalScrollBarPolicy">
           <enum>Qt::ScrollBarAlwaysOff</enum>
          </property>
          <property name="horizontalScrollBarPolicy">
           <enum>Qt::ScrollBarAlwaysOff</enum>
          </property>
          <property name="sizeAdjustPolicy">
           <enum>QAbstractScrollArea::AdjustToContents</enum>
          </property>
          <property name="autoScroll">
           <bool>true</bool>
          </property>
          <attribute name="headerVisible">
           <bool>false</bool>
          </attribute>
          <column>
           <property name="text">
            <string>1</string>
           </property>
          </column>
          <item>
           <property name="text">
            <string>TreeWidget2</string>
           </property>
           <item>
            <property name="text">
             <string>New Item</string>
            </property>
           </item>
           <item>
            <property name="text">
             <string>New Item</string>
            </property>
           </item>
           <item>
            <property name="text">
             <string>New Item</string>
            </property>
           </item>
          </item>
         </widget>
        </item>
        <item>
         <widget class="QTreeWidget" name="treeWidget_3">
          <property name="sizePolicy">
           <sizepolicy hsizetype="Expanding" vsizetype="Minimum">
            <horstretch>0</horstretch>
            <verstretch>0</verstretch>
           </sizepolicy>
          </property>
          <property name="frameShape">
           <enum>QFrame::NoFrame</enum>
          </property>
          <property name="lineWidth">
           <number>0</number>
          </property>
          <property name="verticalScrollBarPolicy">
           <enum>Qt::ScrollBarAlwaysOff</enum>
          </property>
          <property name="horizontalScrollBarPolicy">
           <enum>Qt::ScrollBarAlwaysOff</enum>
          </property>
          <property name="sizeAdjustPolicy">
           <enum>QAbstractScrollArea::AdjustToContents</enum>
          </property>
          <property name="autoScroll">
           <bool>true</bool>
          </property>
          <property name="autoScrollMargin">
           <number>0</number>
          </property>
          <attribute name="headerVisible">
           <bool>false</bool>
          </attribute>
          <column>
           <property name="text">
            <string>1</string>
           </property>
          </column>
          <item>
           <property name="text">
            <string>TreeWidget3</string>
           </property>
           <item>
            <property name="text">
             <string>New Item</string>
            </property>
           </item>
           <item>
            <property name="text">
             <string>New Item</string>
            </property>
           </item>
           <item>
            <property name="text">
             <string>New Item</string>
            </property>
           </item>
           <item>
            <property name="text">
             <string>New Item</string>
            </property>
           </item>
          </item>
         </widget>
        </item>
        <item>
         <spacer name="verticalSpacer">
          <property name="orientation">
           <enum>Qt::Vertical</enum>
          </property>
          <property name="sizeHint" stdset="0">
           <size>
            <width>17</width>
            <height>82</height>
           </size>
          </property>
         </spacer>
        </item>
       </layout>
      </widget>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>241</width>
     <height>24</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections>
  <connection>
   <sender>treeWidget</sender>
   <signal>currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)</signal>
   <receiver>treeWidget_2</receiver>
   <slot>clearSelection()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>33</x>
     <y>42</y>
    </hint>
    <hint type="destinationlabel">
     <x>31</x>
     <y>172</y>
    </hint>
   </hints>
  </connection>
  <connection>
   <sender>treeWidget</sender>
   <signal>currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)</signal>
   <receiver>treeWidget_3</receiver>
   <slot>clearSelection()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>94</x>
     <y>76</y>
    </hint>
    <hint type="destinationlabel">
     <x>43</x>
     <y>271</y>
    </hint>
   </hints>
  </connection>
  <connection>
   <sender>treeWidget_2</sender>
   <signal>currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)</signal>
   <receiver>treeWidget</receiver>
   <slot>clearSelection()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>95</x>
     <y>179</y>
    </hint>
    <hint type="destinationlabel">
     <x>91</x>
     <y>112</y>
    </hint>
   </hints>
  </connection>
  <connection>
   <sender>treeWidget_2</sender>
   <signal>currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)</signal>
   <receiver>treeWidget_3</receiver>
   <slot>clearSelection()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>122</x>
     <y>188</y>
    </hint>
    <hint type="destinationlabel">
     <x>137</x>
     <y>280</y>
    </hint>
   </hints>
  </connection>
  <connection>
   <sender>treeWidget_3</sender>
   <signal>currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)</signal>
   <receiver>treeWidget</receiver>
   <slot>clearSelection()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>193</x>
     <y>296</y>
    </hint>
    <hint type="destinationlabel">
     <x>167</x>
     <y>115</y>
    </hint>
   </hints>
  </connection>
  <connection>
   <sender>treeWidget_3</sender>
   <signal>currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)</signal>
   <receiver>treeWidget_2</receiver>
   <slot>clearSelection()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>217</x>
     <y>295</y>
    </hint>
    <hint type="destinationlabel">
     <x>217</x>
     <y>226</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>

来自 ui-form 的相关翻译代码:

    centralwidget = new QWidget(MainWindow);
    centralwidget->setObjectName(QStringLiteral("centralwidget"));
    verticalLayout = new QVBoxLayout(centralwidget);
    verticalLayout->setSpacing(0);
    verticalLayout->setObjectName(QStringLiteral("verticalLayout"));
    verticalLayout->setSizeConstraint(QLayout::SetMinimumSize);
    verticalLayout->setContentsMargins(0, 0, 0, 0);
    scrollArea = new QScrollArea(centralwidget);
    scrollArea->setObjectName(QStringLiteral("scrollArea"));
    QSizePolicy sizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    sizePolicy.setHorizontalStretch(0);
    sizePolicy.setVerticalStretch(0);
    sizePolicy.setHeightForWidth(scrollArea->sizePolicy().hasHeightForWidth());
    scrollArea->setSizePolicy(sizePolicy);
    scrollArea->setStyleSheet(QStringLiteral("background-color: rgb(255, 255, 255);"));
    scrollArea->setFrameShape(QFrame::NoFrame);
    scrollArea->setLineWidth(0);
    scrollArea->setWidgetResizable(true);
    scrollAreaWidgetContents = new QWidget();
    scrollAreaWidgetContents->setObjectName(QStringLiteral("scrollAreaWidgetContents"));
    scrollAreaWidgetContents->setGeometry(QRect(0, 0, 241, 480));
    verticalLayout_2 = new QVBoxLayout(scrollAreaWidgetContents);
    verticalLayout_2->setSpacing(10);
    verticalLayout_2->setObjectName(QStringLiteral("verticalLayout_2"));
    verticalLayout_2->setContentsMargins(0, 0, 0, 0);
    treeWidget = new QTreeWidget(scrollAreaWidgetContents);
    QTreeWidgetItem *__qtreewidgetitem = new QTreeWidgetItem(treeWidget);
    new QTreeWidgetItem(__qtreewidgetitem);
    new QTreeWidgetItem(__qtreewidgetitem);
    new QTreeWidgetItem(__qtreewidgetitem);
    new QTreeWidgetItem(__qtreewidgetitem);
    new QTreeWidgetItem(__qtreewidgetitem);
    new QTreeWidgetItem(__qtreewidgetitem);
    new QTreeWidgetItem(__qtreewidgetitem);
    new QTreeWidgetItem(__qtreewidgetitem);
    new QTreeWidgetItem(__qtreewidgetitem);
    QTreeWidgetItem *__qtreewidgetitem1 = new QTreeWidgetItem(__qtreewidgetitem);
    QTreeWidgetItem *__qtreewidgetitem2 = new QTreeWidgetItem(__qtreewidgetitem1);
    new QTreeWidgetItem(__qtreewidgetitem2);
    treeWidget->setObjectName(QStringLiteral("treeWidget"));
    QSizePolicy sizePolicy1(QSizePolicy::Expanding, QSizePolicy::Minimum);
    sizePolicy1.setHorizontalStretch(0);
    sizePolicy1.setVerticalStretch(0);
    sizePolicy1.setHeightForWidth(treeWidget->sizePolicy().hasHeightForWidth());
    treeWidget->setSizePolicy(sizePolicy1);
    treeWidget->setFrameShape(QFrame::NoFrame);
    treeWidget->setLineWidth(0);
    treeWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    treeWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    treeWidget->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
    treeWidget->setAlternatingRowColors(false);
    treeWidget->setUniformRowHeights(false);
    treeWidget->setAnimated(false);
    treeWidget->setHeaderHidden(true);
    treeWidget->header()->setVisible(false);

    verticalLayout_2->addWidget(treeWidget);
    //... (other tree-widgets inserted)

   verticalSpacer = new QSpacerItem(17, 82, QSizePolicy::Minimum, QSizePolicy::Expanding);
    verticalLayout_2->addItem(verticalSpacer);
    scrollArea->setWidget(scrollAreaWidgetContents);
    verticalLayout->addWidget(scrollArea);
    MainWindow->setCentralWidget(centralwidget);
4

0 回答 0