0

I have this function in python def:

def niveau(controlName,idNiveau)

I want to connect it to this signal in this way:

QObject.connect(dialog.findChild(QDialogButtonBox, 'buttonBox'), SIGNAL('accepted()'),niveau(control,1))

I got the following error:

Qt signal not callable

Can someone help me with this?

Sparky
  • 98,165
  • 25
  • 199
  • 285

1 Answers1

0

The error you get is because QObject.connect with 3 parameters means:

QObject.connect(QObject, SIGNAL(), callable, Qt.ConnectionType=Qt.AutoConnection)

The third parameter you're passing isn't a callable, but the return value of the call to niveau(control,1).

The arguments a signal is emitted with is determined at emit time, not at connect time. If you would like to speicfy some (or all) parameters at connect time, you can:

  • use functools.partial:

    from functools import partial
    QObject.connect(... , partial(niveau, control, 1))
    
  • use a lambda

    QObject.connect(... , lambda ctrl=control, id=1: niveau(ctrl, id))
    

edit:

By the way, you should really use new style signals - old style signals will not be supported anymore in PyQt5.

mata
  • 67,110
  • 10
  • 163
  • 162
  • Thank you very much for your answer but i use my function (niveau) many time and with differents parameter : QObject.connect(button1... , lambda: niveau(control, 1)) QObject.connect(button2... , lambda: niveau(control, 2)) QObject.connect(button3... , lambda: niveau(control, 3)) so in the result he use onely the last parameter 3 for this case ??? – user2604547 Jul 21 '13 at 22:06
  • You're probably creating your connections in a loop - the problem is that the scope of the lamdbdas is the local scope they are created within. So if a variable changes, it also changes within the lambda, so you'll always see the last value `3`. It's the same behaviour described [here](http://stackoverflow.com/questions/11109838/weird-closure-behavior-in-python). That's why `partial` is preferable here (and I listed it first), it doesn't have this problem. I'll update my answer to account for this. – mata Jul 22 '13 at 09:07