Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt: How to connect QComboBox to function with Arguments

Tags:

python

pyqt

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_())
like image 231
alphanumeric Avatar asked Jan 10 '23 19:01

alphanumeric


1 Answers

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_())
like image 118
alphanumeric Avatar answered Jan 21 '23 09:01

alphanumeric