调整 window 大小时扩展 QTableView 父控件高度

Extend QTableView parent widget height when window is resized

如果 rulesWhiteListFileTBL 高度增加,我想延长 ruleWhiteListWidget_2 的高度。但是正如您在下面的示例代码中看到的那样,QTableView 正在溢出其父窗口小部件。我需要 QTableView 的父级和祖父级高度随着 QTableView 的高度而增加,然后如果高度扩展了 MainWindow,则使用 QScrollArea 滚动。我玩弄了 sizepolicy,但无法弄清楚。

PyQt5 设计器UI代码:

<?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>1112</width>
    <height>718</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <widget class="QScrollArea" name="scrollArea">
   <property name="geometry">
    <rect>
     <x>-10</x>
     <y>0</y>
     <width>1091</width>
     <height>701</height>
    </rect>
   </property>
   <property name="verticalScrollBarPolicy">
    <enum>Qt::ScrollBarAsNeeded</enum>
   </property>
   <property name="widgetResizable">
    <bool>true</bool>
   </property>
   <widget class="QWidget" name="scrollAreaWidgetContents_2">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>1089</width>
      <height>699</height>
     </rect>
    </property>
    <property name="minimumSize">
     <size>
      <width>0</width>
      <height>0</height>
     </size>
    </property>
    <layout class="QVBoxLayout" name="verticalLayout_45">
     <item>
      <layout class="QVBoxLayout" name="verticalLayout_44">
       <item>
        <widget class="QWidget" name="ruleWhiteListWidget" native="true">
         <property name="sizePolicy">
          <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
           <horstretch>0</horstretch>
           <verstretch>0</verstretch>
          </sizepolicy>
         </property>
         <property name="minimumSize">
          <size>
           <width>0</width>
           <height>200</height>
          </size>
         </property>
         <layout class="QHBoxLayout" name="horizontalLayout_17">
          <property name="sizeConstraint">
           <enum>QLayout::SetDefaultConstraint</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>
           <layout class="QHBoxLayout" name="horizontalLayout_16">
            <item>
             <widget class="QWidget" name="ruleWhiteListWidget_2" native="true">
              <property name="sizePolicy">
               <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
                <horstretch>0</horstretch>
                <verstretch>0</verstretch>
               </sizepolicy>
              </property>
              <layout class="QVBoxLayout" name="verticalLayout_43">
               <property name="sizeConstraint">
                <enum>QLayout::SetDefaultConstraint</enum>
               </property>
               <item>
                <layout class="QVBoxLayout" name="verticalLayout_41">
                 <item>
                  <widget class="QPushButton" name="rulesWhiteListFileBTN">
                   <property name="maximumSize">
                    <size>
                     <width>16777215</width>
                     <height>16777215</height>
                    </size>
                   </property>
                   <property name="styleSheet">
                    <string notr="true">#rulesWhiteListFileBTN{
background: #697181;
border: 1px solid #697181;
color: #fff;
font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol;
font-size: 12px;
padding: 6px 16px;
font-weight:600;
}
#rulesWhiteListFileBTN::hover{
background:#41454e;
}</string>
                   </property>
                   <property name="text">
                    <string>Btn 1</string>
                   </property>
                  </widget>
                 </item>
                 <item>
                  <widget class="QTableView" name="rulesWhiteListFileTBL">
                   <property name="minimumSize">
                    <size>
                     <width>0</width>
                     <height>400</height>
                    </size>
                   </property>
                   <property name="maximumSize">
                    <size>
                     <width>16777215</width>
                     <height>16777215</height>
                    </size>
                   </property>
                   <property name="sizeIncrement">
                    <size>
                     <width>0</width>
                     <height>0</height>
                    </size>
                   </property>
                   <property name="styleSheet">
                    <string notr="true">#rulesWhiteListFileTBL{
                color:#fff;
                border:0px;
            }
            QHeaderView::section{
                background-color:#222;
                color:#fff;
                border:0;
            }</string>
                   </property>
                  </widget>
                 </item>
                </layout>
               </item>
              </layout>
             </widget>
            </item>
           </layout>
          </item>
         </layout>
        </widget>
       </item>
       <item>
        <widget class="QWidget" name="rulesEditorWidget" native="true">
         <layout class="QVBoxLayout" name="verticalLayout_69">
          <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>
           <layout class="QVBoxLayout" name="verticalLayout_70">
            <item>
             <widget class="QPlainTextEdit" name="rulesQlineeditor">
              <property name="minimumSize">
               <size>
                <width>0</width>
                <height>400</height>
               </size>
              </property>
              <property name="styleSheet">
               <string notr="true">color:#fff;
font: 14px;
font-family: Monospace;
padding:10px;
border:1px solid #1a1a1a;
border-left:0;</string>
              </property>
             </widget>
            </item>
           </layout>
          </item>
         </layout>
        </widget>
       </item>
      </layout>
     </item>
    </layout>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

您当前的设计有几个多余的容器小部件和布局,它们使事情变得不必要地复杂化。基本上,只要对话框的大小大于 table 和编辑器的组合最小高度,您只希望扩展 table 以占据任何额外的高度;当对话框的大小调整为小于合并的最小值时,您希望出现垂直 scroll-bar。

为了使解决方案更清晰,我已将您的设计减少到绝对最低限度。它只有一个容器小部件(名为 rulesWidget,即 scroll-area 容器小部件)。这反过来只有一个垂直布局设置,可以通过在 属性 编辑器中选择 rulesWidget 并滚动到底部的部分来访问。如果这样做,您会看到 layoutStretch 已设置,这就是 table 占用额外 space 的原因。另请注意,对话框本身也必须设置布局才能使一切正常工作:

新建UI文件:

<?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>645</width>
    <height>695</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout_2">
   <item>
    <widget class="QScrollArea" name="scrollArea">
     <property name="widgetResizable">
      <bool>true</bool>
     </property>
     <widget class="QWidget" name="rulesWidget">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>612</width>
        <height>853</height>
       </rect>
      </property>
      <layout class="QVBoxLayout" name="verticalLayout" stretch="0,1,0">
       <item>
        <widget class="QPushButton" name="rulesWhiteListFileBTN">
         <property name="styleSheet">
          <string notr="true">#rulesWhiteListFileBTN{
background: #697181;
border: 1px solid #697181;
color: #fff;
font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol;
font-size: 12px;
padding: 6px 16px;
font-weight:600;
}
#rulesWhiteListFileBTN::hover{
background:#41454e;
}</string>
         </property>
         <property name="text">
          <string>Btn 1</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QTableView" name="rulesWhiteListFileTBL">
         <property name="minimumSize">
          <size>
           <width>0</width>
           <height>400</height>
          </size>
         </property>
         <property name="styleSheet">
          <string notr="true">#rulesWhiteListFileTBL{
                color:#fff;
                border:0px;
            }
            QHeaderView::section{
                background-color:#222;
                color:#fff;
                border:0;
            }</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QPlainTextEdit" name="rulesQlineeditor">
         <property name="minimumSize">
          <size>
           <width>0</width>
           <height>400</height>
          </size>
         </property>
         <property name="styleSheet">
          <string notr="true">color:#fff;
font: 14px;
font-family: Monospace;
padding:10px;
border:1px solid #1a1a1a;
border-left:0;</string>
         </property>
        </widget>
       </item>
      </layout>
     </widget>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

更新:

要使原始 ui 文件的行为与上述大致相同,最少需要进行以下更改:(1) 在对话框中设置 vertical-layout; (2) 去掉ruleWhiteListWidget上的minimumSize; (3) 在 verticalLayout_44 上将 layoutStretch 设置为 1,0。这是更新后的 ui-file,仅包含以下更改:

<?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>1112</width>
    <height>718</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <item>
    <widget class="QScrollArea" name="scrollArea">
     <property name="verticalScrollBarPolicy">
      <enum>Qt::ScrollBarAsNeeded</enum>
     </property>
     <property name="widgetResizable">
      <bool>true</bool>
     </property>
     <widget class="QWidget" name="scrollAreaWidgetContents_2">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>1079</width>
        <height>873</height>
       </rect>
      </property>
      <property name="minimumSize">
       <size>
        <width>0</width>
        <height>0</height>
       </size>
      </property>
      <layout class="QVBoxLayout" name="verticalLayout_45">
       <item>
        <layout class="QVBoxLayout" name="verticalLayout_44" stretch="1,0">
         <item>
          <widget class="QWidget" name="ruleWhiteListWidget" native="true">
           <property name="sizePolicy">
            <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
             <horstretch>0</horstretch>
             <verstretch>0</verstretch>
            </sizepolicy>
           </property>
           <layout class="QHBoxLayout" name="horizontalLayout_17">
            <property name="sizeConstraint">
             <enum>QLayout::SetDefaultConstraint</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>
             <layout class="QHBoxLayout" name="horizontalLayout_16">
              <item>
               <widget class="QWidget" name="ruleWhiteListWidget_2" native="true">
                <property name="sizePolicy">
                 <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
                  <horstretch>0</horstretch>
                  <verstretch>0</verstretch>
                 </sizepolicy>
                </property>
                <layout class="QVBoxLayout" name="verticalLayout_43">
                 <property name="sizeConstraint">
                  <enum>QLayout::SetDefaultConstraint</enum>
                 </property>
                 <item>
                  <layout class="QVBoxLayout" name="verticalLayout_41">
                   <item>
                    <widget class="QPushButton" name="rulesWhiteListFileBTN">
                     <property name="maximumSize">
                      <size>
                       <width>16777215</width>
                       <height>16777215</height>
                      </size>
                     </property>
                     <property name="styleSheet">
                      <string notr="true">#rulesWhiteListFileBTN{
background: #697181;
border: 1px solid #697181;
color: #fff;
font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol;
font-size: 12px;
padding: 6px 16px;
font-weight:600;
}
#rulesWhiteListFileBTN::hover{
background:#41454e;
}</string>
                     </property>
                     <property name="text">
                      <string>Btn 1</string>
                     </property>
                    </widget>
                   </item>
                   <item>
                    <widget class="QTableView" name="rulesWhiteListFileTBL">
                     <property name="minimumSize">
                      <size>
                       <width>0</width>
                       <height>400</height>
                      </size>
                     </property>
                     <property name="maximumSize">
                      <size>
                       <width>16777215</width>
                       <height>16777215</height>
                      </size>
                     </property>
                     <property name="sizeIncrement">
                      <size>
                       <width>0</width>
                       <height>0</height>
                      </size>
                     </property>
                     <property name="styleSheet">
                      <string notr="true">#rulesWhiteListFileTBL{
                color:#fff;
                border:0px;
            }
            QHeaderView::section{
                background-color:#222;
                color:#fff;
                border:0;
            }</string>
                     </property>
                    </widget>
                   </item>
                  </layout>
                 </item>
                </layout>
               </widget>
              </item>
             </layout>
            </item>
           </layout>
          </widget>
         </item>
         <item>
          <widget class="QWidget" name="rulesEditorWidget" native="true">
           <layout class="QVBoxLayout" name="verticalLayout_69">
            <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>
             <layout class="QVBoxLayout" name="verticalLayout_70">
              <item>
               <widget class="QPlainTextEdit" name="rulesQlineeditor">
                <property name="minimumSize">
                 <size>
                  <width>0</width>
                  <height>400</height>
                 </size>
                </property>
                <property name="styleSheet">
                 <string notr="true">color:#fff;
font: 14px;
font-family: Monospace;
padding:10px;
border:1px solid #1a1a1a;
border-left:0;</string>
                </property>
               </widget>
              </item>
             </layout>
            </item>
           </layout>
          </widget>
         </item>
        </layout>
       </item>
      </layout>
     </widget>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>