2009-11-18 153 views
2

我正在为一个客户开发一个项目,其中设计有一个带有排他选项的单选按钮。Qt Python单选按钮:激活事件

以下是一段运行并显示两个漂亮的单选按钮的代码:

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

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

否则,使用连接应该是另一种选择:

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

但它也不能工作。

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

非常欢迎和赞赏所有评论。

回答

0

这里是解决方案...现在的工作:

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

时有参数bool incl联系到信号,它工作。

+3

piobyz给了你正确信号的名称 - 然后你重新回答自己,并拒绝他的大绿色复选标记?冷,男人。太冷了。 – 2010-11-27 03:24:59

+0

@Brandon Rhodes - piobyz的回答并不完整,因为它只提到了切换(布尔)。但谢谢你的注意,因此我只给了他一个大绿色的检查标记,你可以在这里查看 - 如果你还有其他的东西,请告诉我:http://stackoverflow.com/questions/1753939/qt -python-radiobutton-activate-event – ThreaderSlash 2013-12-14 01:50:24

+0

不,我没有其他的东西在意!并感谢您提供这些额外的信息,显示@ piobyz的答案在进入connect()调用时的样子。谢谢你给他信用,祝你在堆栈溢出的未来工作中运气好! – 2013-12-29 04:30:43

0

看看QButtonGroup类

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)