【问题标题】:Qt Python radiobutton: activate eventQt Python单选按钮:激活事件
【发布时间】:2010-12-17 18:11:37
【问题描述】:

我正在为一位客户开发一个项目,该设计有一个带有专属选项的单选按钮。

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

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

但在选择radiobutton被选中时它没有做任何事情。

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

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

但它也不起作用。

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

所有 cmets 都非常受欢迎和赞赏。

【问题讨论】:

    标签: python user-interface qt qt4 pyqt


    【解决方案1】:

    【讨论】:

    • 链接已失效。
    【解决方案2】:

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

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

    当将参数 bool 包含到切换为信号时,它起作用了。

    【讨论】:

    • piobyz 给了你正确信号的名称——然后你重新回答了自己并拒绝了他的大绿色复选标记?冷,伙计。太冷了。
    • @Brandon Rhodes - piobyz 的答案并不完整,因为它只提到了 toggled(bool)。但是感谢您引起我的注意,因此我只是给了他一个大大的绿色复选标记,您可以在这里查看-如果您还有什么不满意的,请告诉我:stackoverflow.com/questions/1753939/…
    • 不,我不介意其他任何事情!感谢您提供这些额外信息,展示了@piobyz 的答案在进入connect() 电话时的样子。感谢您对他的信任,并祝您在 Stack Overflow 的未来工作中好运!
    【解决方案3】:

    看看QButtonGroup类

    【讨论】:

      【解决方案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)
      

      【讨论】:

        猜你喜欢
        • 2014-01-07
        • 1970-01-01
        • 2016-06-27
        • 2014-12-12
        • 1970-01-01
        • 2014-05-28
        • 2012-02-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多