Bob Sacamano
Bob Sacamano

Reputation: 439

group widgets inside groupbox

I have a group box that looks like this:

enter image description here

And here is the code that creates it:

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

As one can see the plus buttons are near the combo boxes but far from the checkboxes. How can I group the comboboxes and the buttons together so they will span across all of the line edits and labels beneath them?

Basically I want to achieve something like this:

enter image description here

Upvotes: 1

Views: 4355

Answers (1)

eyllanesc
eyllanesc

Reputation: 244282

The solution is to use QHBoxLayout for the QComboBox with the QPushButton and change the QSizePolicy of QPushButton so that it takes the sizeHint() as maximum.

   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

enter image description here

Upvotes: 1

Related Questions