简体   繁体   中英

Qt Python radiobutton: activate event

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.

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.

# 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)

看看QButtonGroup类

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM