PySide6 QGroupBox Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# A group box provides a frame, a title on top
# and displays various other widgets inside itself.
import sys
from PySide6.QtWidgets import (QApplication,
QWidget, QHBoxLayout, QVBoxLayout,
QGroupBox, QRadioButton, QLabel)
class Window(QWidget):
def __init__(self):
super().__init__()
layout = QHBoxLayout()
self.label = QLabel()
self.label.setFixedWidth(80)
# 1 - Create the group box
# and add a layout to it. You can't
# add widgets directly to the group box.
self.groupbox = QGroupBox()
self.groupbox.setTitle('Group box')
groupbox_layout = QVBoxLayout()
self.groupbox.setLayout(groupbox_layout)
# 2 - Add widgets to the layout.
self.radiobutton_1 = QRadioButton('Option 1')
self.radiobutton_2 = QRadioButton('Set checkable')
self.radiobutton_3 = QRadioButton('Set non-checkable')
groupbox_layout.addWidget(self.radiobutton_1)
groupbox_layout.addWidget(self.radiobutton_2)
groupbox_layout.addWidget(self.radiobutton_3)
# 4- Connect child widget signals with the slot
self.radiobutton_1.toggled.connect(self.on_toggled)
self.radiobutton_2.toggled.connect(self.on_toggled)
self.radiobutton_3.toggled.connect(self.on_toggled)
self.radiobutton_1.setChecked(True)
layout.addWidget(self.groupbox)
layout.addWidget(self.label)
self.setLayout(layout)
# 3 - Add slot method. If there are
# multiple radio buttons in a group box
# only one can be checked, unlike checkboxes.
def on_toggled(self):
if self.radiobutton_1.isChecked():
self.label.setText(self.radiobutton_1.text())
elif self.radiobutton_2.isChecked():
self.label.setText(self.radiobutton_2.text())
self.groupbox.setCheckable(True)
else:
self.label.setText(self.radiobutton_3.text())
self.groupbox.setCheckable(False)
if __name__ == '__main__':
app = QApplication(sys.argv)
main_window = Window()
main_window.show()
sys.exit(app.exec())
QGroupBox
is a widget that has a frame and a title on top and allows you to display various other widgets inside itself but is commonly used to group checkboxes or radiobuttons. QGroupBox
does not lay out its child widgets automatically - you need to do it yourself using one of the Qt layout classes. You can set a QGroupBox
to be checkable
which lets the user enable or disable all its child widgets simultaneously. To use a group box in your application
-
Create a
QGroupBox
object and add a layout to it as you can’t add widgets directly. In the example we use aQVBoxLayout
. -
Create widgets and add them to the layout. In the example we add three
QRadioButton
s. We also set one of the radio buttons tochecked
. -
Create the slot method to handle
QRadioButton.toggled
signals. The slot sets a label’s text to the text of the checked radiobutton and the last two radiobuttons also toggle the group boxtogglable
property. -
Connect
QRadioButton.toggled
signals with the slot.