Python Forum

Full Version: how to deselect radiobutton in pyqt5
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello;
I created radiobuttons (radioButton_degre, radioButton_ddm), checkBox(checkbox_confirmed) and a button "Reset" (pushButton_reset) with Qt Designer

when I click on the button, I want all radiobouttons and checkbox to become unselected.
the fonctions display_degre_unit and display_ddm_unit are not defined, to reduce the code for a simplified code

#!/usr/bin/python3
# -*- coding: utf-8 -*-

# PyQT5 :
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *


class MainApp(QMainWindow,FORM_CLASS) :
    def __init__(self,parent=None) :
        super(MainApp,self).__init__(parent)
        QMainWindow.__init__(self)
        self.setupUi(self)
		
		self.pushButton_reset.clicked.connect(self.initialize)
        self.radioButton_degre.toggled.connect(self.display_degre_unit)
        self.radioButton_ddm.toggled.connect(self.display_ddm_unit)

	def initialize(self):

		self.checkbox_confirmed.setCheckState(Qt.Unchecked)
		self.radioButton_degre.setChecked(False)
		self.radioButton_ddm.setChecked(False)
		

def main():
    app=QApplication(sys.argv)
    window=MainApp()
    window.show()
    app.exec_()

if __name__=='__main__' :
    main()
It works for the checkbox but does not work for radiobuttons

thanks for help
Okay well because you appear to be using the Designer [for some unholy reason ;)] this is not an MRE as you cannot run it and determine what is going on -- however you could have created a simple MRE by doing things properly (aka not using that horrible designer code) as follows -- also I have included the fix to your issue which I got by doing a simple google search for pyqt QRadioButton setChecked false not working
from sys import exit as sysExit

from PyQt5.QtCore    import *
from PyQt5.QtGui     import *
from PyQt5.QtWidgets import *
  
class MainApp(QWidget) :
    def __init__(self,parent=None) :
# Do not use 'Super( )' unless you fully understand what it is for
# as its benefit will actually occur less often than its pitfalls    
        QWidget.__init__(self)

      # Removing this bit of useless code
      #  self.setupUi(self)
      
        self.pushButton_reset = QPushButton('Reset')
        self.pushButton_reset.clicked.connect(self.initialize)

        self.radioButton_degre = QRadioButton('Degrees')
        self.radioButton_degre.toggled.connect(self.display_degre_unit)

        self.radioButton_ddm = QRadioButton('DDM Units')
        self.radioButton_ddm.toggled.connect(self.display_ddm_unit)

        self.RadioGroup = QButtonGroup()
        self.RadioGroup.addButton(self.radioButton_degre)
        self.RadioGroup.addButton(self.radioButton_ddm)       

        self.checkbox_confirmed = QCheckBox('Confirmed')
        
        HBox = QHBoxLayout()
        HBox.addWidget(self.pushButton_reset)
        HBox.addWidget(QLabel('     '))  # Simple horizontal spacer
        HBox.addWidget(self.radioButton_degre)
        HBox.addWidget(self.radioButton_ddm)
        HBox.addWidget(QLabel('   '))  # Simple horizontal spacer
        HBox.addWidget(self.checkbox_confirmed)
        HBox.addStretch(1)
        
        VBox = QVBoxLayout()
        VBox.addLayout(HBox)
        VBox.addStretch(1)
        
        self.setLayout(VBox)
 
    def initialize(self):
        self.RadioGroup.setExclusive(False)

        self.radioButton_degre.setChecked(False)
        self.radioButton_ddm.setChecked(False)

        self.RadioGroup.setExclusive(True)

        self.checkbox_confirmed.setCheckState(Qt.Unchecked)

    def display_degre_unit(self):
        pass

    def display_ddm_unit(self):
        pass

if __name__=='__main__' :
    app=QApplication([])
    window=MainApp()
    window.show()
    sysExit(app.exec_())
to solve the problem; I proceeded as follows: I group my radiobuttons in a QButtonGroup into Qt Designer, like that :

Quote:"Select the ones you want to group, and right click on one of them. Then, you'll find an option to "group" the buttons. If you click that, you will notice that a new QButtonGroup has appeared in your object browser in designer. You can select that button group to give it a name, make it exclusive"

Then, I integrated the following code in my function ;

self.checkbox_confirmed .setCheckState(Qt.Unchecked)
self.buttonGroup_unite_mesure.setExclusive(False)
self.radioButton_degre.setChecked(False)
self.radioButton_ddm.setChecked(False)

self.buttonGroup_unite_mesure.setExclusive(True)
thanks for the previous code posted by Denni, which gave me an idea how to solve my problem
Well you can continue to use the Designer and enjoy having continuous headaches down the road it is your choice -- however I have taught numerous Designer coders how to create a normal PyQt program and everyone of them (some slower than others) have come to realize just how much of a headache it is to use the Designer over coding PyQt as it was meant to be used. So I am fine with you beating your head on that proverbial brick wall but if you should ever get tired of giving yourself a headache just shoot me message (somehow) and I will be more than happy to help you learn the proper way to code PyQt --- by the way I think the Designer was actually meant to be used to make Web Pages not PyQt python code -- so unless your creating web pages you are definitely using a tool meant for some other purpose.