Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a widget into QGroupBox

I am trying to add a QPushButton widget into a QGroupBox such as:

self.btn = QtGui.QPushButton('Push Button')
self.grp_box = QtGui.QGroupBox('Button Section')
self.grp_box.addWidget(self.btn)

When trying to run the code, I got this error : AttributeError: 'NoneType' object has no attribute 'addWidget'

After some online checking, it seems that QGroupBox only allows setLayout, meaning I will need to use QVBoxLayout or QHBoxLayout etc.

Is there anyway to get around this, adding in a widget without the use of any layout(s)? I am using PyQt.

like image 240
dissidia Avatar asked Nov 20 '22 03:11

dissidia


1 Answers

First create your main layout = QHBoxLayout()

  main_layout = QHBoxLayout()

Then create the group box:

  group_box = QGroupBox("Group Box")

Create the group box layout:

  group_box_layout = QVBoxLayout()

Add widgets to the group box layout like this:

  group_box_layout.addWidget(QCheckBox("Check Box 1"))
  group_box_layout.addWidget(QCheckBox("Check Box 2"))
  group_box_layout.addWidget(QCheckBox("Check Box 3"))

Assign the group box layout to the group box:

  group_box.setLayout(group_box_layout)

Assing the group box to the main layout:

  main_layout.addWidget(group_box)

And add this at the end:

    widget = QWidget()
    widget.setLayout(layout1)
    self.setCentralWidget(widget)
like image 136
Eskimo868 Avatar answered Dec 05 '22 19:12

Eskimo868