I am writing a program with python 3.3.3 and pyqt5. I have connected many signals and slots with no problem. This one is causing a problem. My code follows:
Building on ekhumoro's answer, you could also let the signal pass the currentText to the lambda function. This means you would just pass the text into the function and not have to get the currentText later.
def _vendorChanged(vendorText, vendorsDict, modelCBox):
modelsList = vendorsDict[vendorText]
added_models = []
modelCBox.clear()
for rclass in modelsList:
if rclass.MODEL not in added_models:
added_models.append(rclass.MODEL)
print("adding to modelCB")
modelCBox.addItems(sorted(added_models))
print("Done adding to modelCB")
vendorComboBox.currentTextChanged[str].connect(
lambda vendorText: _vendorChanged(vendorText, dictVendors, modelComboBox))
Also, if you don't need the references to dictVendors and modelComboBox to update based on the current scope of the lambda function every time the signal is emitted, you could leave them out of the parameter list and let the _vendorChanged function simply inherit them from it's parent scope (which is the same as the lambda's parent scope... so I'm not sure there's be any difference...). The appeal of this is that you no longer need the lamda to provide the signal with a callable... you can give it the _vendorChanged function directly:
def _vendorChanged(vendorText):
modelsList = dictVendors[vendorText]
added_models = []
modelComboBox.clear()
for rclass in modelsList:
if rclass.MODEL not in added_models:
added_models.append(rclass.MODEL)
print("adding to modelCB")
modelComboBox.addItems(sorted(added_models))
print("Done adding to modelCB")
vendorComboBox.currentTextChanged[str].connect(_vendorChanged)
Hope that helps!
Excellent answers but just want to add this explanation:
Doing
vendorComboBox.currentTextChanged.connect(_vendorChanged(vendorComboBox, dictVendors, modelComboBox))
is the same as doing
obj = _vendorChanged(vendorComboBox, dictVendors, modelComboBox)
vendorComboBox.currentTextChanged.connect(obj)
Now you should see that obj
is not a function but the result of calling your _vendorChanged
function with the 3 parameters. This means that you get the impression that the signal is immediately fired, thereby calling your function, but in fact it's just your function executing as directed. The second problem is that _vendorChanged
does not return anything so obj is in fact None. Since you are trying to connect a signal to None, you get this error. The solution is given in other answers.
The error is caused by attempting to connect to the result of a function call (which in this case is None
), instead of the function object itself. Of course, this also explains why the function is executed immedaitely.
You should wrap the function call in a lambda
, like this:
vendorComboBox.currentTextChanged.connect(
lambda: _vendorChanged(vendorComboBox, dictVendors, modelComboBox))