【问题标题】:PySide: Connecting QComboBox indices with stringsPySide:用字符串连接 QComboBox 索引
【发布时间】:2014-12-21 05:47:59
【问题描述】:

我想将 QComboBox 索引与特定字符串连接(即,当我选择“A”时,我希望它打印“A 已被选择”,当我选择“B”时,“B 已被选择” )。

我是 PySide 和学习的新手,所以我确信存在一个简单的解决方案。帮助表示赞赏。

from PySide import QtGui


class Widget(QtGui.QWidget):

    def __init__(self, parent=None):
        super().__init__(parent)

        v_global_layout = QtGui.QVBoxLayout()

        method_selection = QtGui.QComboBox()
        method_selection.addItem("A")
        method_selection.addItem("B")

        v_global_layout.addWidget(method_selection)
        self.setLayout(v_global_layout)

        def do_somethinh():
            print("A has been selected!!!")
        method_selection.activated.connect(do_somethinh)


if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)

    main_window = Widget()
    main_window.setGeometry(100, 100, 640, 480)
    main_window.show()

    sys.exit(app.exec_())

【问题讨论】:

    标签: python pyqt pyside signals-slots qcombobox


    【解决方案1】:

    QComboBox.activated 信号有两个重载:一个发送所选项目的索引,另一个发送其文本。默认重载发送索引。要选择另一个重载,您需要稍微不同的语法:

        def do_somethinh(text):
            print(text, "has been selected!!!")
        method_selection.activated[str].connect(do_somethinh)
    

    所以信号对象具有__getitem__ 支持,它允许您通过将参数类型作为键传递来选择信号的特定重载(如果有多个参数,则可以传递类型元组)。

    【讨论】:

      猜你喜欢
      • 2018-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-23
      • 2015-06-15
      • 1970-01-01
      相关资源
      最近更新 更多