首页 > 解决方案 > 设计师重叠问题

问题描述

我遇到小部件和布局在我的滚动区域内开始重叠的问题。我当前的布局应该是这样的:

我的布局:
我的布局

但最终像这样,小部件开始重叠(这是固定大小的组框):

重叠示例:
重叠示例

或者这个(没有固定大小的分组框):

“Sqished”小部件和推出视图小部件:

我尝试设置固定尺寸和最小尺寸以及更改尺寸策略,但无论我做什么,一些小部件都会调整大小,有些会重叠,有些会被推到视野之外。

我正在使用设计器和 PyUIC。

如果有帮助,这是我的层次结构: Qt 设计器层次结构:
Qt 设计器层次结构

设计器生成的 .ui 文件:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>299</width>
    <height>478</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QGridLayout" name="gridLayout">
   <property name="margin">
    <number>0</number>
   </property>
   <item row="0" column="0">
    <widget class="QScrollArea" name="scrollArea">
     <property name="baseSize">
      <size>
       <width>0</width>
       <height>0</height>
      </size>
     </property>
     <property name="widgetResizable">
      <bool>true</bool>
     </property>
     <widget class="QWidget" name="scrollAreaWidgetContents">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>295</width>
        <height>474</height>
       </rect>
      </property>
      <property name="minimumSize">
       <size>
        <width>0</width>
        <height>410</height>
       </size>
      </property>
      <layout class="QVBoxLayout" name="verticalLayout_5">
       <item>
        <layout class="QVBoxLayout" name="verticalLayout">
         <property name="spacing">
          <number>6</number>
         </property>
         <property name="sizeConstraint">
          <enum>QLayout::SetDefaultConstraint</enum>
         </property>
         <item>
          <widget class="QLabel" name="label_2">
           <property name="layoutDirection">
            <enum>Qt::LeftToRight</enum>
           </property>
           <property name="autoFillBackground">
            <bool>false</bool>
           </property>
           <property name="text">
            <string>Select a .bed file to open</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="BrowseForm" name="browseWidget"/>
         </item>
         <item>
          <widget class="QGroupBox" name="groupBox_2">
           <property name="title">
            <string>Bead selection mode</string>
           </property>
           <layout class="QGridLayout" name="gridLayout_2">
            <item row="1" column="0">
             <widget class="QRadioButton" name="radioStart">
              <property name="text">
               <string>Start</string>
              </property>
              <attribute name="buttonGroup">
               <string notr="true">beadSelectionModeGroup</string>
              </attribute>
             </widget>
            </item>
            <item row="0" column="1">
             <widget class="QRadioButton" name="radioInRangeStrict">
              <property name="text">
               <string>Strictly in range</string>
              </property>
              <attribute name="buttonGroup">
               <string notr="true">beadSelectionModeGroup</string>
              </attribute>
             </widget>
            </item>
            <item row="0" column="0">
             <widget class="QRadioButton" name="radioInRange">
              <property name="text">
               <string>In range</string>
              </property>
              <property name="checked">
               <bool>true</bool>
              </property>
              <attribute name="buttonGroup">
               <string notr="true">beadSelectionModeGroup</string>
              </attribute>
             </widget>
            </item>
            <item row="1" column="3">
             <widget class="QRadioButton" name="radioEnd">
              <property name="text">
               <string>End</string>
              </property>
              <attribute name="buttonGroup">
               <string notr="true">beadSelectionModeGroup</string>
              </attribute>
             </widget>
            </item>
            <item row="1" column="1">
             <widget class="QRadioButton" name="radioMiddle">
              <property name="text">
               <string>Middle</string>
              </property>
              <attribute name="buttonGroup">
               <string notr="true">beadSelectionModeGroup</string>
              </attribute>
             </widget>
            </item>
           </layout>
          </widget>
         </item>
         <item>
          <widget class="QGroupBox" name="groupBox">
           <property name="sizePolicy">
            <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
             <horstretch>0</horstretch>
             <verstretch>0</verstretch>
            </sizepolicy>
           </property>
           <property name="minimumSize">
            <size>
             <width>0</width>
             <height>0</height>
            </size>
           </property>
           <property name="baseSize">
            <size>
             <width>0</width>
             <height>150</height>
            </size>
           </property>
           <property name="title">
            <string>Colour options</string>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_3">
            <item>
             <widget class="QRadioButton" name="radioSingleColor">
              <property name="text">
               <string>Use single colour</string>
              </property>
              <property name="checked">
               <bool>true</bool>
              </property>
              <attribute name="buttonGroup">
               <string notr="true">colourButtonGroup</string>
              </attribute>
             </widget>
            </item>
            <item>
             <widget class="QRadioButton" name="radioScoreColor">
              <property name="text">
               <string>Use score for colours</string>
              </property>
              <attribute name="buttonGroup">
               <string notr="true">colourButtonGroup</string>
              </attribute>
             </widget>
            </item>
            <item>
             <widget class="QRadioButton" name="radioColorColor">
              <property name="text">
               <string>Use colour from file</string>
              </property>
              <attribute name="buttonGroup">
               <string notr="true">colourButtonGroup</string>
              </attribute>
             </widget>
            </item>
            <item>
             <widget class="QStackedWidget" name="stackedWidget">
              <property name="sizePolicy">
               <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
                <horstretch>0</horstretch>
                <verstretch>0</verstretch>
               </sizepolicy>
              </property>
              <property name="minimumSize">
               <size>
                <width>0</width>
                <height>92</height>
               </size>
              </property>
              <property name="baseSize">
               <size>
                <width>100</width>
                <height>100</height>
               </size>
              </property>
              <property name="currentIndex">
               <number>1</number>
              </property>
              <widget class="QWidget" name="page">
               <property name="sizePolicy">
                <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
                 <horstretch>0</horstretch>
                 <verstretch>0</verstretch>
                </sizepolicy>
               </property>
               <property name="minimumSize">
                <size>
                 <width>0</width>
                 <height>0</height>
                </size>
               </property>
               <layout class="QVBoxLayout" name="verticalLayout_2">
                <property name="topMargin">
                 <number>0</number>
                </property>
                <item>
                 <layout class="QHBoxLayout" name="horizontalLayout_2">
                  <property name="sizeConstraint">
                   <enum>QLayout::SetDefaultConstraint</enum>
                  </property>
                  <item>
                   <widget class="QLabel" name="label_5">
                    <property name="text">
                     <string>Colour</string>
                    </property>
                   </widget>
                  </item>
                  <item>
                   <widget class="QColourPicker" name="colorPicker">
                    <property name="sizePolicy">
                     <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
                      <horstretch>0</horstretch>
                      <verstretch>0</verstretch>
                     </sizepolicy>
                    </property>
                    <property name="minimumSize">
                     <size>
                      <width>23</width>
                      <height>23</height>
                     </size>
                    </property>
                    <property name="maximumSize">
                     <size>
                      <width>23</width>
                      <height>23</height>
                     </size>
                    </property>
                    <property name="baseSize">
                     <size>
                      <width>23</width>
                      <height>23</height>
                     </size>
                    </property>
                    <property name="styleSheet">
                     <string notr="true"/>
                    </property>
                    <property name="text">
                     <string>n</string>
                    </property>
                   </widget>
                  </item>
                  <item>
                   <spacer name="horizontalSpacer">
                    <property name="orientation">
                     <enum>Qt::Horizontal</enum>
                    </property>
                    <property name="sizeHint" stdset="0">
                     <size>
                      <width>40</width>
                      <height>20</height>
                     </size>
                    </property>
                   </spacer>
                  </item>
                 </layout>
                </item>
                <item>
                 <spacer name="verticalSpacer_2">
                  <property name="orientation">
                   <enum>Qt::Vertical</enum>
                  </property>
                  <property name="sizeHint" stdset="0">
                   <size>
                    <width>20</width>
                    <height>40</height>
                   </size>
                  </property>
                 </spacer>
                </item>
               </layout>
              </widget>
              <widget class="QWidget" name="page_2">
               <layout class="QVBoxLayout" name="verticalLayout_9">
                <property name="topMargin">
                 <number>0</number>
                </property>
                <item>
                 <layout class="QHBoxLayout" name="horizontalLayout">
                  <item>
                   <layout class="QVBoxLayout" name="verticalLayout_8">
                    <item>
                     <widget class="QComboBox" name="scoreOrPercentile">
                      <item>
                       <property name="text">
                        <string>Score</string>
                       </property>
                      </item>
                      <item>
                       <property name="text">
                        <string>Percentile</string>
                       </property>
                      </item>
                     </widget>
                    </item>
                    <item>
                     <widget class="QLabel" name="label_3">
                      <property name="text">
                       <string>Colour gradient</string>
                      </property>
                     </widget>
                    </item>
                   </layout>
                  </item>
                  <item>
                   <layout class="QVBoxLayout" name="verticalLayout_7">
                    <item>
                     <widget class="QLineEdit" name="startGradient">
                      <property name="text">
                       <string>0</string>
                      </property>
                     </widget>
                    </item>
                    <item>
                     <widget class="QColourPicker" name="colorPickerStartGradient">
                      <property name="sizePolicy">
                       <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
                        <horstretch>0</horstretch>
                        <verstretch>0</verstretch>
                       </sizepolicy>
                      </property>
                      <property name="minimumSize">
                       <size>
                        <width>23</width>
                        <height>23</height>
                       </size>
                      </property>
                      <property name="maximumSize">
                       <size>
                        <width>23</width>
                        <height>23</height>
                       </size>
                      </property>
                      <property name="baseSize">
                       <size>
                        <width>23</width>
                        <height>23</height>
                       </size>
                      </property>
                      <property name="styleSheet">
                       <string notr="true"/>
                      </property>
                      <property name="text">
                       <string>n</string>
                      </property>
                     </widget>
                    </item>
                   </layout>
                  </item>
                  <item>
                   <layout class="QVBoxLayout" name="verticalLayout_6">
                    <item>
                     <widget class="QLineEdit" name="endGradient">
                      <property name="text">
                       <string>1000</string>
                      </property>
                     </widget>
                    </item>
                    <item>
                     <widget class="QColourPicker" name="colorPickerEndGradient">
                      <property name="sizePolicy">
                       <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
                        <horstretch>0</horstretch>
                        <verstretch>0</verstretch>
                       </sizepolicy>
                      </property>
                      <property name="minimumSize">
                       <size>
                        <width>23</width>
                        <height>23</height>
                       </size>
                      </property>
                      <property name="maximumSize">
                       <size>
                        <width>23</width>
                        <height>23</height>
                       </size>
                      </property>
                      <property name="baseSize">
                       <size>
                        <width>23</width>
                        <height>23</height>
                       </size>
                      </property>
                      <property name="styleSheet">
                       <string notr="true"/>
                      </property>
                      <property name="text">
                       <string>n</string>
                      </property>
                     </widget>
                    </item>
                   </layout>
                  </item>
                 </layout>
                </item>
                <item>
                 <widget class="QCheckBox" name="colourBlendCheckBox_2">
                  <property name="text">
                   <string>Use colour blend</string>
                  </property>
                 </widget>
                </item>
                <item>
                 <layout class="QHBoxLayout" name="horizontalLayout_4">
                  <item>
                   <widget class="QLabel" name="label_8">
                    <property name="text">
                     <string>Conflict colour</string>
                    </property>
                   </widget>
                  </item>
                  <item>
                   <widget class="QColourPicker" name="conflictColorPicker_2">
                    <property name="sizePolicy">
                     <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
                      <horstretch>0</horstretch>
                      <verstretch>0</verstretch>
                     </sizepolicy>
                    </property>
                    <property name="minimumSize">
                     <size>
                      <width>23</width>
                      <height>23</height>
                     </size>
                    </property>
                    <property name="maximumSize">
                     <size>
                      <width>23</width>
                      <height>23</height>
                     </size>
                    </property>
                    <property name="baseSize">
                     <size>
                      <width>23</width>
                      <height>23</height>
                     </size>
                    </property>
                    <property name="styleSheet">
                     <string notr="true"/>
                    </property>
                    <property name="text">
                     <string>n</string>
                    </property>
                   </widget>
                  </item>
                  <item>
                   <spacer name="horizontalSpacer_3">
                    <property name="orientation">
                     <enum>Qt::Horizontal</enum>
                    </property>
                    <property name="sizeHint" stdset="0">
                     <size>
                      <width>40</width>
                      <height>20</height>
                     </size>
                    </property>
                   </spacer>
                  </item>
                 </layout>
                </item>
                <item>
                 <spacer name="verticalSpacer_3">
                  <property name="orientation">
                   <enum>Qt::Vertical</enum>
                  </property>
                  <property name="sizeHint" stdset="0">
                   <size>
                    <width>0</width>
                    <height>0</height>
                   </size>
                  </property>
                 </spacer>
                </item>
               </layout>
              </widget>
              <widget class="QWidget" name="page_3">
               <layout class="QVBoxLayout" name="verticalLayout_4">
                <property name="topMargin">
                 <number>0</number>
                </property>
                <item>
                 <widget class="QCheckBox" name="colourBlendCheckBox">
                  <property name="text">
                   <string>Use colour blend</string>
                  </property>
                 </widget>
                </item>
                <item>
                 <layout class="QHBoxLayout" name="horizontalLayout_3">
                  <item>
                   <widget class="QLabel" name="label_4">
                    <property name="text">
                     <string>Conflict colour</string>
                    </property>
                   </widget>
                  </item>
                  <item>
                   <widget class="QColourPicker" name="conflictColorPicker">
                    <property name="sizePolicy">
                     <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
                      <horstretch>0</horstretch>
                      <verstretch>0</verstretch>
                     </sizepolicy>
                    </property>
                    <property name="minimumSize">
                     <size>
                      <width>23</width>
                      <height>23</height>
                     </size>
                    </property>
                    <property name="maximumSize">
                     <size>
                      <width>23</width>
                      <height>23</height>
                     </size>
                    </property>
                    <property name="baseSize">
                     <size>
                      <width>23</width>
                      <height>23</height>
                     </size>
                    </property>
                    <property name="styleSheet">
                     <string notr="true"/>
                    </property>
                    <property name="text">
                     <string>n</string>
                    </property>
                   </widget>
                  </item>
                  <item>
                   <spacer name="horizontalSpacer_2">
                    <property name="orientation">
                     <enum>Qt::Horizontal</enum>
                    </property>
                    <property name="sizeHint" stdset="0">
                     <size>
                      <width>40</width>
                      <height>20</height>
                     </size>
                    </property>
                   </spacer>
                  </item>
                 </layout>
                </item>
                <item>
                 <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>
               </layout>
              </widget>
             </widget>
            </item>
           </layout>
          </widget>
         </item>
         <item>
          <layout class="QFormLayout" name="formLayout">
           <item row="2" column="0">
            <widget class="QLabel" name="label">
             <property name="text">
              <string>Main model ID</string>
             </property>
            </widget>
           </item>
           <item row="2" column="1">
            <widget class="QLineEdit" name="mainModelId">
             <property name="text">
              <string>1</string>
             </property>
            </widget>
           </item>
           <item row="3" column="0">
            <widget class="QLabel" name="label_6">
             <property name="text">
              <string>New model name</string>
             </property>
            </widget>
           </item>
           <item row="3" column="1">
            <widget class="QLineEdit" name="modelName">
             <property name="text">
              <string>bed_model</string>
             </property>
            </widget>
           </item>
           <item row="1" column="1">
            <widget class="QCheckBox" name="hideOrg">
             <property name="text">
              <string/>
             </property>
             <property name="checked">
              <bool>false</bool>
             </property>
            </widget>
           </item>
           <item row="1" column="0">
            <widget class="QLabel" name="label_7">
             <property name="text">
              <string>Hide beads on main model</string>
             </property>
            </widget>
           </item>
          </layout>
         </item>
         <item>
          <widget class="QPushButton" name="generateModelButton">
           <property name="text">
            <string>Generate Model</string>
           </property>
          </widget>
         </item>
        </layout>
       </item>
      </layout>
     </widget>
    </widget>
   </item>
  </layout>
 </widget>
 <customwidgets>
  <customwidget>
   <class>QColourPicker</class>
   <extends>QPushButton</extends>
   <header>.qcolourpicker.h</header>
  </customwidget>
  <customwidget>
   <class>BrowseForm</class>
   <extends>QLineEdit</extends>
   <header>.browseform.h</header>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections>
  <connection>
   <sender>colourButtonGroup</sender>
   <signal>buttonClicked(int)</signal>
   <receiver>stackedWidget</receiver>
   <slot>setCurrentIndex(int)</slot>
   <hints>
    <hint type="sourcelabel">
     <x>-1</x>
     <y>-1</y>
    </hint>
    <hint type="destinationlabel">
     <x>217</x>
     <y>264</y>
    </hint>
   </hints>
  </connection>
  <connection>
   <sender>colourBlendCheckBox</sender>
   <signal>toggled(bool)</signal>
   <receiver>label_4</receiver>
   <slot>setDisabled(bool)</slot>
   <hints>
    <hint type="sourcelabel">
     <x>81</x>
     <y>215</y>
    </hint>
    <hint type="destinationlabel">
     <x>74</x>
     <y>222</y>
    </hint>
   </hints>
  </connection>
  <connection>
   <sender>colourBlendCheckBox</sender>
   <signal>toggled(bool)</signal>
   <receiver>conflictColorPicker</receiver>
   <slot>setDisabled(bool)</slot>
   <hints>
    <hint type="sourcelabel">
     <x>111</x>
     <y>215</y>
    </hint>
    <hint type="destinationlabel">
     <x>104</x>
     <y>245</y>
    </hint>
   </hints>
  </connection>
 </connections>
 <buttongroups>
  <buttongroup name="colourButtonGroup"/>
  <buttongroup name="beadSelectionModeGroup"/>
 </buttongroups>
</ui>

更新:非常感谢您快速而全面的回答。对于后代,导致我的问题的主要错误是我不知道将最小大小设置回 0x0 与重置它不同。重置我的滚动区域和组框的最小尺寸修复了它!

标签: pythonpyqtqt-designer

解决方案


您已经为scrollAreaWidgetContents滚动区域的容器设置了最小高度。这会导致覆盖minimumSizeHint容器(使用其所有项目所需的最小尺寸计算并确保正确显示所有小部件),因此滚动区域强制它小于它应该小于它直到达到最小高度,即使这会使某些小部件无法使用甚至不可见。

完全重置该属性(使用最小尺寸字段的红色小箭头按钮),小部件将仅调整为它们自己的最小默认尺寸。

另请注意,您设置了许多其他与大小相关的属性,除非确实需要,否则不应设置:

  • baseSize属性仅对顶级窗口有用,如果sizeIncrement已设置,则它不是小部件的“建议”大小(取决于小部件的sizeHint;重置所有这些属性,因为它们对子小部件无用;
  • 重置所有minimumSize设置为的属性,0x0或者对于容器小部件,这些属性设置为可能不允许其子项正确显示的大小(与滚动区域相同);
  • 重置大小策略覆盖,除非你真的知道它们是如何工作的;
  • 重置间隔的大小提示,因为堆叠的小部件已经处理了所有页面所需的最小尺寸,因此使用间隔的最小高度可能会导致未使用的空白空间;

上面的所有重置指示都意味着使用红色小箭头按钮,它实际上将属性重置为小部件的默认状态。请注意,手动将值设置为默认值与重置它不同:属性的默认值可能取决于不同的方面,并且对于所有小部件和情况(包括操作系统/样式)可能并不相同,并且虽然使用默认值(属性未设置)确保 Qt 正确使用默认的预期行为,但显式设置值可能不会产生相同的结果,即使它看起来是默认值。

进一步的建议:

  • scrollAreaWidgetContents已经有一个垂直布局集,但是您正在使用几乎没用的进一步嵌套的垂直布局将每个小部件从该嵌套布局移动到滚动区域的主要区域并删除该布局;
  • 表单布局通常建议用于扩展(可能是动态)界面;您可以在底部使用普通的网格布局,因为它只包含 3 个小部件,并且您还应该设置复选框文本而不是使用标签(复选框的标准约定说它们的标签应该在右侧);
  • 检查标签顺序,因为它看起来有点混乱;

推荐阅读