Frage

QComboBox is connected to a function using following syntax:

myComboBox.activated.connect(self.myFunction )

But I need to be able to send the arguments from ComboBox to myFunction(). But if I use:

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

I am getting

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

What syntax needs to be used to connect a QComboBox to a function that is able to receive arguments sent from Comobobox?

EDITED LATER:

Here is the code resulting an 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_())
War es hilfreich?

Lösung

After I posted a question Stachoverflow suggested a link which explained a lot. Here is the answer:

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

Andere Tipps

Inference

In here the activated.connect(yourFunc) after passing through decorator internally by library, yourFunc has two arguments without using partial from func tools that is self,val. Now when use pars from functools, decorator puts the self,passed arguments,val in yourFunc. Note the order of the arguments with partial and without partial

    # Warning code must go inside extended QWidget class.
    QCombox.activated.connect(yourFunc)
    #! above line will pass two arguments to YourFunc function, so while accessing you can,
    def yourFunc(self,val):
        print(val)

    #________

    from funcTools import partial as pars
    comboList = ["Male","Female"]
    comboBox = QCombox.addItems(combolist)
    comboBox.activated.connect(pars(yourFuncWithCustomArguments,comboList))
    #! above line will pass two arguments to YourFuncWithCArgs function, socWithCustomArgume while accessing you can,
    def yourFuncWithCustomArguments(self,comboList:list[str],val):
        print(comboList[val])

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top