如何在将布局设置为其父级后防止窗口小部件收缩?

问题描述 投票:0回答:1

我在Centralwidget中并排有一个QGroupBox和QTabWidget,并且希望QGroupBox具有固定的宽度和扩展的高度,并且希望QTabWidget具有扩展的宽度和高度。

当我为中央部件设置水平布局时,出现了问题。一切正常,但我的QGroupBox宽度缩小。

如何在保持小部件的可扩展性的同时防止这种缩小的发生?

“之前”

“之后”

带有最小的.ui文件的示例:

<?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>640</width>
    <height>313</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QHBoxLayout" name="horizontalLayout">
    <item>
     <widget class="QGroupBox" name="groupBox">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Fixed" vsizetype="Expanding">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
      <property name="title">
       <string>GroupBox</string>
      </property>
      <widget class="QLabel" name="label">
       <property name="geometry">
        <rect>
         <x>10</x>
         <y>140</y>
         <width>231</width>
         <height>16</height>
        </rect>
       </property>
       <property name="text">
        <string>This text is supposed to be visible in its entirety.</string>
       </property>
      </widget>
     </widget>
    </item>
    <item>
     <widget class="QTabWidget" name="tabWidget">
      <widget class="QWidget" name="tab">
       <attribute name="title">
        <string>Tab 1</string>
       </attribute>
      </widget>
      <widget class="QWidget" name="tab_2">
       <attribute name="title">
        <string>Tab 2</string>
       </attribute>
      </widget>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>640</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>
python pyqt pyqt5 qt-designer
1个回答
0
投票
现在,您没有为左侧分组框设置任何布局,因此每个分组框子级都是“自由的”,并且无论如何在理论上都可以移动和调整大小。同时,可以根据需要调整groupbox的大小,因为它没有任何限制:在主窗口的主布局中,您还有一个QTabWidget(这是一个自动尝试扩展到最大可用空间的小部件) ),并且由于groupbox没有任何约束,因此结果是,选项卡小部件将占据大部分宽度,但groupbox所需的最小宽度(其框架和标题)除外。

要解决您的问题,请为组框设置一个布局,它将自动调整大小以正确显示其内容:确保

至少组框中存在一个小部件,右键单击该组的空白区域框,然后在“布局”子菜单中选择一个布局管理器。

© www.soinside.com 2019 - 2024. All rights reserved.