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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With