QGroupBox

../_images/4_10_qgroupbox_sample.png

As shown above, the QGroupBox widget provides a group box frame with a title.


Example

import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QGroupBox, QRadioButton, QCheckBox, QPushButton, QMenu, QGridLayout, QVBoxLayout)


class MyApp(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):

        grid = QGridLayout()
        grid.addWidget(self.createFirstExclusiveGroup(), 0, 0)
        grid.addWidget(self.createSecondExclusiveGroup(), 1, 0)
        grid.addWidget(self.createNonExclusiveGroup(), 0, 1)
        grid.addWidget(self.createPushButtonGroup(), 1, 1)

        self.setLayout(grid)

        self.setWindowTitle('Box Layout')
        self.setGeometry(300, 300, 480, 320)
        self.show()

    def createFirstExclusiveGroup(self):
        groupbox = QGroupBox('Exclusive Radio Buttons')

        radio1 = QRadioButton('Radio1')
        radio2 = QRadioButton('Radio2')
        radio3 = QRadioButton('Radio3')
        radio1.setChecked(True)

        vbox = QVBoxLayout()
        vbox.addWidget(radio1)
        vbox.addWidget(radio2)
        vbox.addWidget(radio3)
        groupbox.setLayout(vbox)

        return groupbox

    def createSecondExclusiveGroup(self):
        groupbox = QGroupBox('Exclusive Radio Buttons')
        groupbox.setCheckable(True)
        groupbox.setChecked(False)

        radio1 = QRadioButton('Radio1')
        radio2 = QRadioButton('Radio2')
        radio3 = QRadioButton('Radio3')
        radio1.setChecked(True)
        checkbox = QCheckBox('Independent Checkbox')
        checkbox.setChecked(True)

        vbox = QVBoxLayout()
        vbox.addWidget(radio1)
        vbox.addWidget(radio2)
        vbox.addWidget(radio3)
        vbox.addWidget(checkbox)
        vbox.addStretch(1)
        groupbox.setLayout(vbox)

        return groupbox

    def createNonExclusiveGroup(self):
        groupbox = QGroupBox('Non-Exclusive Checkboxes')
        groupbox.setFlat(True)

        checkbox1 = QCheckBox('Checkbox1')
        checkbox2 = QCheckBox('Checkbox2')
        checkbox2.setChecked(True)
        tristatebox = QCheckBox('Tri-state Button')
        tristatebox.setTristate(True)

        vbox = QVBoxLayout()
        vbox.addWidget(checkbox1)
        vbox.addWidget(checkbox2)
        vbox.addWidget(tristatebox)
        vbox.addStretch(1)
        groupbox.setLayout(vbox)

        return groupbox

    def createPushButtonGroup(self):
        groupbox = QGroupBox('Push Buttons')
        groupbox.setCheckable(True)
        groupbox.setChecked(True)

        pushbutton = QPushButton('Normal Button')
        togglebutton = QPushButton('Toggle Button')
        togglebutton.setCheckable(True)
        togglebutton.setChecked(True)
        flatbutton = QPushButton('Flat Button')
        flatbutton.setFlat(True)
        popupbutton = QPushButton('Popup Button')
        menu = QMenu(self)
        menu.addAction('First Item')
        menu.addAction('Second Item')
        menu.addAction('Third Item')
        menu.addAction('Fourth Item')
        popupbutton.setMenu(menu)

        vbox = QVBoxLayout()
        vbox.addWidget(pushbutton)
        vbox.addWidget(togglebutton)
        vbox.addWidget(flatbutton)
        vbox.addWidget(popupbutton)
        vbox.addStretch(1)
        groupbox.setLayout(vbox)

        return groupbox


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())

We made four group boxes according to the type of button.


Description

grid = QGridLayout()
grid.addWidget(self.createFirstExclusiveGroup(), 0, 0)
grid.addWidget(self.createSecondExclusiveGroup(), 1, 0)
grid.addWidget(self.createNonExclusiveGroup(), 0, 1)
grid.addWidget(self.createPushButtonGroup(), 1, 1)

self.setLayout(grid)

Use the grid layout to place the group boxes.

Group boxes created through each method are placed in each position via addWidget().


def createFirstExclusiveGroup(self):
    groupbox = QGroupBox('Exclusive Radio Buttons')

    radio1 = QRadioButton('Radio1')
    radio2 = QRadioButton('Radio2')
    radio3 = QRadioButton('Radio3')
    radio1.setChecked(True)

    vbox = QVBoxLayout()
    vbox.addWidget(radio1)
    vbox.addWidget(radio2)
    vbox.addWidget(radio3)
    groupbox.setLayout(vbox)

    return groupbox

createFirstExclusiveGroup() method creates a group box with exclusive radio buttons.

The method first creates a groupbox and a button, and then places it through the vertical box layout.

Finally, set the vertical box layout(vbox) as the layout of the group box.

We made three radio buttons and placed them vertically.


def createSecondExclusiveGroup(self):
    groupbox = QGroupBox('Exclusive Radio Buttons')
    groupbox.setCheckable(True)
    groupbox.setChecked(False)

createSecondExclusiveGroup() method creates a group box with three radio buttons and one check box.

You can also make groupbox selectable by using the setCheckable() method.


def createNonExclusiveGroup(self):
    groupbox = QGroupBox('Non-Exclusive Checkboxes')
    groupbox.setFlat(True)

Create NonExclusiveGroup() method creates a group box with non-exclusive check boxes.

Use setFlat() to make the group box look flat.


def createPushButtonGroup(self):
    groupbox = QGroupBox('Push Buttons')
    groupbox.setCheckable(True)
    groupbox.setChecked(True)

createPushButtonGroup() method creates a group box with multiple push buttons.

Use setCheckable() and setChecked() to make the group box selectable and selected when run.


Results

../_images/4_10_qgroupbox.png ../_images/4_10_qgroupbox_mac.png

Figure 5-10. Make Groupbox widget.


Prev/Next

Prev :
Next :