python - 将布局设置为其父级后如何防止小部件收缩?
问题描述
我在 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>
解决方案
向父级添加小部件不足以使父级调整自身大小以使子级变得完全可见。布局管理器不仅确保所有小部件都正确调整大小,而且设置布局的小部件知道它们是否需要比实际提供的更多(或更少)空间。
现在,您没有为左侧 groupbox 设置任何布局,因此每个 groupbox 子项都是“自由的”,理论上无论如何都可以移动和调整大小。同时,如果需要,可以调整 groupbox 的大小,因为它没有任何约束:在主窗口的主布局中,您还有一个 QTabWidget(这是一个自动尝试扩展到最大可用空间的小部件),并且由于 groupbox 没有任何约束,结果是选项卡小部件将占据大部分宽度,除了 groupbox 所需的最小值(其框架和标题)。
要解决您的问题,请为 groupbox 设置布局,它将自动调整大小以正确显示其内容:确保groupbox中至少存在一个小部件,右键单击框的空白区域并选择其中一个“布局”子菜单中的布局管理器。
推荐阅读
- pyspark - 不匹配的输入“何时”Pyspark 但语句工作时的其他情况
- php - Laravel 导入错误。应用程序在我的电脑上运行,但在同事的电脑上崩溃
- passenger - Rails 6 Bcrypt 与生产中的乘客错误
- flutter - 当我点击搜索按钮时,它会显示带有一些脏错误的红屏
- api - Firefox - 编辑和重新发送格式?
- firebase - ref.push().key & ref.child (类型不匹配)
- html - 无法居中容器
- html - 更改 CSS 样式
- python-3.x - python中受保护变量和公共变量有什么区别
- apache-kafka - 如何测试 WindowStore 保留期?