1

我有一个看起来像这样的组框:

在此处输入图像描述

这是创建它的代码:

def create_settings_group(self):
   group_box_settings = QtGui.QGroupBox(self)
   group_box_settings.setTitle("Settings")

   grid = QtGui.QGridLayout()

   grid.addWidget(self.label_1, 3, 0)
   grid.addWidget(self.label_2, 3, 2)
   grid.addWidget(self.label_3, 0, 0)
   grid.addWidget(self.label_4, 1, 0)
   grid.addWidget(self.label_6, 2, 0)
   grid.addWidget(self.label_7, 3, 4)
   grid.addWidget(self.label_8, 3, 6)
   grid.addWidget(self.label_9, 3, 8)

   grid.addWidget(self.line_edit_1, 0, 1, 1, 9)
   grid.addWidget(self.combo_box_1, 1, 1, 1, 8)
   grid.addWidget(self.push_button_1, 1, 9)
   grid.addWidget(self.combo_box_2, 2, 1, 1, 8)
   grid.addWidget(self.push_button_2, 2, 9)
   grid.addWidget(self.line_edit_2, 3, 1)
   grid.addWidget(self.line_edit_3, 3, 3)
   grid.addWidget(self.line_edit_4, 3, 5)
   grid.addWidget(self.line_edit_5, 3, 7)
   grid.addWidget(self.line_edit_6, 3, 9)

   grid.addWidget(self.check_box_1, 0, 10)
   grid.addWidget(self.check_box_2, 1, 10)
   grid.addWidget(self.check_box_3, 2, 10)

   group_box_settings.setLayout(grid)

   return group_box_settings

可以看到加号按钮靠近组合框,但远离复选框。如何将组合框和按钮组合在一起,以便它们跨越它们下方的所有行编辑和标签?

基本上我想实现这样的目标:

在此处输入图像描述

4

1 回答 1

1

解决方案是使用QHBoxLayoutforQComboBoxQPushButton更改QSizePolicyofQPushButton以使其取sizeHint()最大值。

   def create_settings_group(self):
      group_box_settings = QtGui.QGroupBox(self)
      group_box_settings.setTitle("Settings")

      grid = QtGui.QGridLayout()

      grid.addWidget(self.label_1, 3, 0)
      grid.addWidget(self.label_2, 3, 2)
      grid.addWidget(self.label_3, 0, 0)
      grid.addWidget(self.label_4, 1, 0)
      grid.addWidget(self.label_6, 2, 0)
      grid.addWidget(self.label_7, 3, 4)
      grid.addWidget(self.label_8, 3, 6)
      grid.addWidget(self.label_9, 3, 8)

      grid.addWidget(self.line_edit_1, 0, 1, 1, 9)

      self.push_button_1.setSizePolicy(QtGui.QSizePolicy.Maximum, QtGui.QSizePolicy.Fixed)
      hlay1 = QtGui.QHBoxLayout()
      hlay1.addWidget(self.combo_box_1)      
      hlay1.addWidget(self.push_button_1)
      grid.addLayout(hlay1, 1, 1, 1, 9)

      self.push_button_2.setSizePolicy(QtGui.QSizePolicy.Maximum, QtGui.QSizePolicy.Fixed)
      hlay2 = QtGui.QHBoxLayout()
      hlay2.addWidget(self.combo_box_2)
      hlay2.addWidget(self.push_button_2)
      grid.addLayout(hlay2, 2, 1, 1, 9)

      grid.addWidget(self.line_edit_2, 3, 1)
      grid.addWidget(self.line_edit_3, 3, 3)
      grid.addWidget(self.line_edit_4, 3, 5)
      grid.addWidget(self.line_edit_5, 3, 7)
      grid.addWidget(self.line_edit_6, 3, 9)

      grid.addWidget(self.check_box_1, 0, 10)
      grid.addWidget(self.check_box_2, 1, 10)
      grid.addWidget(self.check_box_3, 2, 10)

      group_box_settings.setLayout(grid)

      return group_box_settings

在此处输入图像描述

于 2018-10-22T07:06:33.823 回答