pyqt5qradiobutton

Why are 2 sets of separate QRadioButtons conflicting with each other?


I’m trying to use 2 sets of QRadioButton(s) that select different attributes.

The snippet of the code reads:

self.imum_box = QHBoxLayout()
pigmemti_box = QHBoxLayout() # First set of QRadioButtons for attribute ‘Font color’ 
self.nuntium_1 = QLabel("Font color")
pigmemti_box.addWidget(self.nuntium_1)
self.t_albinus = QRadioButton("White",self)
self.t_albinus.setChecked(True)
self.t_albinus.toggled.connect(self.pigmemtum_has_changed)
self.t_nigreos = QRadioButton("Black",self)
self.t_nigreos.toggled.connect(self.pigmemtum_has_changed)
pigmemti_box.addWidget(self.t_albinus)
pigmemti_box.addWidget(self.t_nigreos)
self.etiqueta = QLabel("   ")
pigmemti_box.addWidget(self.etiqueta)
palette_box = QHBoxLayout() # Second set of QRadioButtons for attribute ‘Color palette’
self.nuntium_2 = QLabel("Color palette")
palette_box.addWidget(self.nuntium_2)
self.red_blue = QRadioButton("Blue and red",self)
self.red_blue.setChecked(True)
self.red_blue.toggled.connect(self.palette_has_changed)
palette_box.addWidget(self.red_blue)
self.echo_tops = QRadioButton("Echo tops",self)
self.echo_tops.setChecked(False)
self.echo_tops.toggled.connect(self.palette_has_changed)
palette_box.addWidget(self.echo_tops)
self.black_white = QRadioButton("Black and white",self)
self.black_white.setChecked(False)
self.black_white.toggled.connect(self.palette_has_changed)
palette_box.addWidget(self.black_white)
self.imum_box.addLayout(pigmemti_box)
self.imum_box.addLayout(palette_box)
self.tab1.setLayout(self.imum_box)  # This line just adds the 2 set to the general layout

The problem is that even though the QRadioButton(s) belong to different sets (and different layouts), they are still interfering. For example, if I check on ‘White’ , it will uncheck ‘Blue and red’ even though they are not supposed to be connected. The same problem happens if I check ‘Echo tops’ as it will uncheck ‘White’ or ‘Black.’ Not sure why this behavior.

Addendum: I also tried QButtonGroup with the following snippet:

self.imum_box = QHBoxLayout()
classis_pigmemti = QButtonGroup()
pigmemti_box = QHBoxLayout()
self.nuntium_1 = QLabel("Font color")
pigmemti_box.addWidget(self.nuntium_1)
self.t_albinus = QRadioButton("White",self)
self.t_albinus.setChecked(True)
self.t_nigreos = QRadioButton("Black",self)
pigmemti_box.addWidget(self.t_albinus)
pigmemti_box.addWidget(self.t_nigreos)
classis_pigmemti.addButton(self.t_albinus)
classis_pigmemti.addButton(self.t_nigreos)
palette_box = QHBoxLayout()
classis_palette = QButtonGroup()
self.nuntium_2 = QLabel("Color palette")
palette_box.addWidget(self.nuntium_2)
self.red_blue = QRadioButton("Blue and red",self)
self.red_blue.setChecked(True)
palette_box.addWidget(self.red_blue)
self.echo_tops = QRadioButton("Echo tops",self)
self.echo_tops.setChecked(False)
palette_box.addWidget(self.echo_tops)
self.black_white = QRadioButton("Black and white",self)
self.black_white.setChecked(False)
palette_box.addWidget(self.black_white)
classis_palette.addButton(self.red_blue)
classis_palette.addButton(self.echo_tops)
classis_palette.addButton(self.black_white)
self.imum_box.addLayout(pigmemti_box)
self.imum_box.addLayout(palette_box)
self.tab1.setLayout(self.imum_box)

However, this didn’t work either as it is possible to select 2 elements from the same set with none from the other.


Solution

  • The docs state that by default all radiobuttons that belong to the same parent widget behave like one group. All your buttons have the same parent: self. Putting the buttons in different layouts has no influence on this.

    If you want your "sets" to be independent of eachother, you should put them in separate QButtonGroups

    from PyQt5.QtWidgets import *
    
    class Test(QWidget):
        def __init__(self):
            super().__init__()
            group_1 = QButtonGroup(self)
            group_2 = QButtonGroup(self)
    
            rb_1 = QRadioButton("1-a")
            rb_2 = QRadioButton("1-b")
            rb_3 = QRadioButton("2-a")
            rb_4 = QRadioButton("2-b")
    
            rb_1.setChecked(True)
            rb_3.setChecked(True)
    
            group_1.addButton(rb_1)
            group_1.addButton(rb_2)
            group_2.addButton(rb_3)
            group_2.addButton(rb_4)
    
            layout = QGridLayout(self)
            layout.addWidget(rb_1, 0, 0)
            layout.addWidget(rb_2, 0, 1)
            layout.addWidget(rb_3, 1, 0)
            layout.addWidget(rb_4, 1, 1)
    
    app = QApplication([])
    w = Test()
    w.show()
    app.exec()