Home > Mobile >  setChecked radiobutton of another group pyqt
setChecked radiobutton of another group pyqt

Time:11-23

I have 2 radiobuttons created (inside a QMainWindow class) like:

    def dtype_radiobuttons(self):
        layout = QHBoxLayout()
        rb1 = QRadioButton("complex")
        rb1.toggled.connect(lambda: self.update_image("dtype", rb1.text()))


        self.real_dtype_rb = QRadioButton("real", self)
        self.real_dtype_rb.toggled.connect(lambda: self.update_image("dtype", self.real_dtype_rb.text()))

        self.btngroup.append(QButtonGroup())
        self.btngroup[-1].addButton(self.real_dtype_rb)
        self.btngroup[-1].addButton(rb1)

        rb1.setChecked(True)
        layout.addWidget(rb1)
        layout.addWidget(self.real_dtype_rb)
        layout.addStretch()

        return layout

    def library_radiobutton(self):
        layout = QHBoxLayout()
        self.cvnn_library_rb = QRadioButton("cvnn", self)
        self.cvnn_library_rb.toggled.connect(lambda: self.update_image("library", self.cvnn_library_rb.text()))

        rb2 = QRadioButton("tensorflow", self)
        rb2.toggled.connect(lambda: self.update_image("library", rb2.text()))

        self.btngroup.append(QButtonGroup())
        self.btngroup[-1].addButton(rb2)
        self.btngroup[-1].addButton(self.cvnn_library_rb)

        self.cvnn_library_rb.setChecked(True)
        layout.addWidget(self.cvnn_library_rb)
        layout.addWidget(rb2)
        layout.addStretch()

        return layout

I want to make it impossible to select the complex option of the dtype radiobuttons group and tensorflow radiobutton of the library radiobuttons. Leaving 3 out of the 4 possible combinations. So if I select complex and library was tensorflow, I want to automatically change the library to cvnn. I tried to implement it like this:

    def update_image(self, key, value):
        if value == "complex":
            if hasattr(self, 'cvnn_library_rb'):    # It wont exists if I still didnt create the radiobutton.
                self.cvnn_library_rb.setChecked(True)   # Set library cvnn
        elif value == "tensorflow":
            if hasattr(self, 'real_dtype_rb'):
                self.real_dtype_rb.setChecked(True)   # Set real dtype
        ... Do the other stuff I need to do.

The weird thing is that it actually works in the sense that, for example, if I am on complex activated and select tensorflow, the radiobutton changes to real (what I want!) but tensorflow does not get selected! I need to select it again as if making self.real_dtype_rb.setChecked(True) cancels the selection of the radiobutton I clicked on. (Very weird if you ask me).


  • The hasattr is used because depending on the order I call the functions, there are some radiobuttons that will be created before the other, so it might not exist.

  • This is an option I am considering but it's disabling the radiobutton group instead of changing their state (not what I prefer).

CodePudding user response:

The signal toggled is triggered whenever you change the state of your radio buttons. So, it will be triggered when you call setChecked (once for the radio button you toggle and once for the other you untoggle) and update_image is called is the wrong case.

You have to check the state of the radio button and call update_image only if the radio button is toggled:

rb2.toggled.connect(lambda state: state and self.update_image("library", rb2.text(), state))
  • Related