Qt Python radiobutton: activate event

2019-05-17 07:37发布

问题:

I am developing a project for one customer, where the design has a radio button with exclusive options.

Here is a piece of the code that runs and show two nice radio buttons:

    self.performGroupBox = QtGui.QGroupBox(self.centralwidget)
    self.performGroupBox.setGeometry(QtCore.QRect(50, 20, 181, 121))
    self.performGroupBox.setObjectName("performGroupBox")     

    self.consultRadioButton = QtGui.QRadioButton(self.performGroupBox)
    self.consultRadioButton.setGeometry(QtCore.QRect(40, 30, 84, 18))
    self.consultRadioButton.setObjectName("consultRadioButton")

    self.insertRadioButton = QtGui.QRadioButton(self.performGroupBox)
    self.insertRadioButton.setGeometry(QtCore.QRect(40, 60, 84, 18))
    self.insertRadioButton.setObjectName("insertRadioButton")

it just looks like:

perform:
    () Consult
    () Insert

The point here is, how to know what choice was marked: "consultRadioButton" or "insertRadioButton"?

Here is a sample on trying to get this information:

    if self.consultRadioButton.isChecked():
        self.call_Consult()
    if self.insertRadioButton.isChecked():
        self.call_Insert()

But it didn't do anything when the radiobutton is chosen.

Otherwise, using connect should be another option:

    QtCore.QObject.connect(self.consultRadioButton, QtCore.SIGNAL("currentIndexChanged(QString)"), self.call_Consult)  
    QtCore.QObject.connect(self.insertRadioButton, QtCore.SIGNAL("currentIndexChanged(QString)"), self.call_Insert) 

But it didn't work either.

What is missing here... Any suggestion?

All comments are highly welcome and appreciated.

回答1:

Try this signal instead:

void toggled (bool)

https://doc.qt.io/qt-5/qabstractbutton.html#toggle

And example usage: https://www.tutorialspoint.com/pyqt/pyqt_qradiobutton_widget.htm



回答2:

Here is solution... now working:

QtCore.QObject.connect(self.radioButton1,QtCore.SIGNAL("toggled(bool)"),self.radio_activateInput)

when have the parameter bool included into toggled to signal, it worked.



回答3:

Take a look at QButtonGroup class



回答4:

# Assuming 'self' is a QtGui object
self.consultRadioButton = QtGui.QRadioButton('Consult')
# I prefer layout managers, but that is another topic
self.consultRadioButton.setGeometry(QtCore.QRect(40, 30, 84, 18))
self.consultRadioButton.setObjectName("consultRadioButton")

self.insertRadioButton = QtGui.QRadioButton('Insert')
self.insertRadioButton.setGeometry(QtCore.QRect(40, 60, 84, 18))
self.insertRadioButton.setObjectName("insertRadioButton")

# Set Default
self.consultRadioButton.setChecked(True)

# Create a Group and make it exclusive
self.methodGrp.setExclusive(True)

# Add radio buttons to group
self.methodGrp.addButton(self.consultRadioButton)
self.methodGrp.addButton(self.insertRadioButton)

# Connect Event handlers
self.consultRadioButton.clicked.connect(self.callConsult)
self.insertRadioButton.clicked.connect(self.callInsert)