You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PySide6(Qt for Python)中QGridLayout行拉伸问题求助

QGridLayout 行高均分解决方案

问题

开发PySide6 GUI应用时,QGridLayout默认第0行占据大部分空间,其余行挤在页面底部。尝试用setRowStretch()设置相同拉伸值但未生效,需要让每行占用相同空间。

解决方法

要让QGridLayout的所有行均分空间,需确保为每一行设置相同的拉伸因子,且拉伸因子的设置要在布局添加完所有控件后执行。

拉伸因子决定了行在布局可用空间中的分配比例,所有行设为相同值(比如1)时,它们会平分所有额外空间。

修改后的代码

class ToolSelectionWidget(QWidget):
    def __init__(self):
        super().__init__()
        tool_selection_layout = QGridLayout()

        # Load tool checkbox
        self.load_tool_box = QCheckBox("Load grinding wheel")
        self.load_tool_box.setCheckState(Qt.Checked)
        self.load_tool_box.stateChanged.connect(
            self.load_tool_box_state_changed)

        # Wheel Choice
        self.wheel_choice_combobox = QComboBox()
        self.wheel_choice_combobox.addItems(list_wheel_folders(some_path))

        # Save Checkbox
        self.save_as_template_checkbox = QCheckBox("Save wheel as template")
        self.save_as_template_checkbox.setCheckState(Qt.Unchecked)
        self.save_as_template_checkbox.setEnabled(False)

        # Wheel Parameters
        self.inner_radius = QLineEdit()
        self.inner_radius.setEnabled(False)
        self.outer_radius = QLineEdit()
        self.outer_radius.setEnabled(False)
        self.number_of_grains = QLineEdit()
        self.number_of_grains.setEnabled(False)
        self.random_seed = QLineEdit()
        self.random_seed.setEnabled(False)
        self.template_number = QLineEdit()
        self.template_number.setEnabled(False)
        self.tool_selection_title = QLabel(
            "Grinding wheel selection/ creation")

        # Add Widgets to Layout
        # Row 0
        tool_selection_layout.addWidget(
            self.tool_selection_title, 0, 0)
        # Row 1
        tool_selection_layout.addWidget(self.load_tool_box, 1, 0)
        tool_selection_layout.addWidget(self.wheel_choice_combobox, 1, 1)

        # Row 2
        tool_selection_layout.addWidget(QLabel("Inner Radius:"), 2, 0)
        tool_selection_layout.addWidget(self.inner_radius, 2, 1)
        tool_selection_layout.addWidget(QLabel("Outer Radius:"), 2, 2)
        tool_selection_layout.addWidget(self.outer_radius, 2, 3)
        tool_selection_layout.addWidget(QLabel("Number of Grains:"), 2, 4)
        tool_selection_layout.addWidget(self.number_of_grains, 2, 5)
        tool_selection_layout.addWidget(QLabel("Random Seed:"), 2, 6)
        tool_selection_layout.addWidget(self.random_seed, 2, 7)

        # Row 3
        tool_selection_layout.addWidget(self.save_as_template_checkbox, 3, 0)
        tool_selection_layout.addWidget(QLabel("Template Number:"), 3, 1)
        tool_selection_layout.addWidget(self.template_number, 3, 2)

        # 设置所有行拉伸因子为1,均分空间
        for row in range(4):
            tool_selection_layout.setRowStretch(row, 1)

        # set Layout
        self.setLayout(tool_selection_layout)

补充说明

  • 如果部分控件有固定的最小高度,可能会导致行高无法完全均分,此时可以调整控件的minimumHeight属性,或者使用setMinimumHeight(0)允许控件压缩。
  • 确保ToolSelectionWidget所在的父布局或窗口没有固定高度限制,否则拉伸效果可能无法体现。

内容的提问来源于stack exchange,提问作者lreinberg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 04:17:07