PyQt:如何连接QComboBox以使用Arguments

时间:2014-04-16 17:57:40

标签: python pyqt

QComboBox使用以下语法连接到函数:

myComboBox.activated.connect(self.myFunction )

但我需要能够将参数从ComboBox发送到myFunction()。但如果我使用:

myComboBox.activated.connect(self.myFunction(myArg1, myArg2 )

我正在

TypeError: connect() slot argument should be a callable or a signal, not 'NoneType'

需要使用什么语法将QComboBox连接到能够接收从Comobobox发送的参数的函数?

以后编辑:

以下是产生TypeError的代码:

connect() slot argument should be a callable or a signal, not 'NoneType'


from PyQt4 import QtCore, QtGui
import sys

class MyClass(object):
    def __init__(self, arg):
        super(MyClass, self).__init__()
        self.arg = arg        

class myWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(myWindow, self).__init__(parent)

        self.comboBox = QtGui.QComboBox(self)
        self.comboBox.addItems([str(x) for x in range(3)])

        self.myObject=MyClass(id(self) )

        self.comboBox.activated.connect(self.myFunction(self.myObject, 'someArg'))

    def myFunction(self, arg1=None, arg2=None):
        print '\n\t myFunction(): ', type(arg1),type(arg2)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('myApp')
    dialog = myWindow()
    dialog.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:5)

我发布了一个问题后,Stachoverflow提出了一个解释很多的链接。这是答案:

from PyQt4 import QtCore, QtGui

class MyClass(object):
    def __init__(self, arg):
        super(MyClass, self).__init__()
        self.arg = arg        

class myWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(myWindow, self).__init__(parent)

        self.comboBox = QtGui.QComboBox(self)
        self.comboBox.addItems([str(x) for x in range(3)])

        self.myObject=MyClass(id(self) )

        slotLambda = lambda: self.indexChanged_lambda(self.myObject)
        self.comboBox.currentIndexChanged.connect(slotLambda)

    @QtCore.pyqtSlot(str)
    def indexChanged_lambda(self, string):
        print 'lambda:', type(string), string

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('myApp')
    dialog = myWindow()
    dialog.show()
    sys.exit(app.exec_())