[PyQt] pyqt4topyqt5 a helper for the conversion PyQt4 to PyQt5

Baz Walter bazwal at ftml.net
Tue Jul 23 17:01:18 BST 2013


On 23/07/13 09:24, Vincent Vande Vyvre wrote:
> I know some signal send an unexpected boolean but I've never seen a
> problem about that.
> In all my own codes, for a signal, say:
>
>    button.clicked.connect(self.foo)
>
> You can have the callable:
>
>    def foo(self, arg)
> or:
>    def foo(self)
>
> without raise any error.

You've completely missed the point :)

If you look carefully at what I posted you will see that there is a real 
problem. To spell it out using the example from your README:

     ui.right.clicked.connect(lambda angle=90: self.rotate(angle))

The signal will pass 'False' by default, so the 'angle' argument of the 
handler will be effectively set to '0' every time the button is clicked, 
rather than the '90' that was intended.

This is different from the old-style signal:

     self.connect(ui.right, SIGNAL("clicked()"), lambda angle=90: 
self.rotate(angle))

where the default is to pass nothing, meaning that the 'angle' argument 
will remain as the intended '90'.

The examples you give above don't suffer from this problem, because they 
don't have *default arguments*. The problem case is this:

     def foo(self, arg=25)

Personally, I always use the 'clicked[()]' form when connecting up these 
kinds of signals, because it's so easy to add a default argument to a 
handler at a later stage and forget about the overloads of the signal 
that pass a default value.

(Yeah, I know that '[()]' looks ugly - but the potential for bugs is far 
uglier).

-- 
Regards
Baz Walter


More information about the PyQt mailing list