Qt Python单选按钮:激活事件

4

我正在为一位客户开发项目,该设计具有互斥选项的单选按钮。

这是一段运行代码,显示两个漂亮的单选按钮:

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

它看起来就像:

perform:
    () Consult
    () Insert

这里的关键是,如何知道选择了哪个选项:"consultRadioButton"还是"insertRadioButton"?
以下是获取此信息的示例:
    if self.consultRadioButton.isChecked():
        self.call_Consult()
    if self.insertRadioButton.isChecked():
        self.call_Insert()

但是当选择单选按钮时,它没有做任何事情。

否则,使用connect应该是另一个选项:

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

但是它仍然没有起作用。

这里缺少什么...有什么建议吗?

欢迎并感谢所有的评论。

4个回答

10

链接现在已经失效。 - Pavel V.

1

这是解决方案...现在可以工作了:

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

当将参数bool包含到toggled信号中时,它可以正常工作。


3
piobyz给了你正确信号的名称,然后你重新回答并拒绝给他打大绿色勾勾?冷酷啊,伙计。 - Brandon Rhodes
@Brandon Rhodes - piobyz的回答不够完整,因为它只提到了toggled(bool)。但是感谢你引起了我的注意,所以我给了他一个大大的绿色勾号,你可以在这里查看:https://dev59.com/zErSa4cB1Zd3GeqPV18r 如果你还有其他问题,请告诉我。 - ThreaderSlash
不,我没有任何意见!感谢您提供这些额外信息,展示了@piobyz的答案在connect()调用中的样子。感谢您给他信用,并祝愿您在Stack Overflow上的未来工作顺利! - Brandon Rhodes

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

0

看一下 QButtonGroup 类


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接