[PyKDE] Auto-connecting Slots

Phil Thompson phil at riverbankcomputing.co.uk
Sat Jan 28 10:20:13 GMT 2006


On Saturday 28 January 2006 12:31 am, Giovanni Bajo wrote:
> Phil Thompson <phil at riverbankcomputing.co.uk> wrote:
> > The QtCore.signature() decorator takes a single argument which is, in
> > effect, the C++ signature of the method which tells the auto-connect
> > code which signal to connect. For example...
> >
> >     @QtCore.signature("on_spinbox_valueChanged(int)")
> >     def on_spinbox_valueChanged(self, value):
> >         # value will only ever be an integer.
> >
> > See the calculatorform.py example.
>
> Notice that naming it "signature" is a namespace violation from those using
> "from QtCore import *". OTOH, you shouldn't impose this code style of
> prefixing each and every name with the QtModule name: I personally hate it,
> and I can't see why Python code should get 3 times more verbose than the
> corresponding C++ code. Having 'Q' at the beginning of the word is enough
> namespace for me and for every C++ Qt project out there. Specifically, Qt
> itself does not expose anything but symbols which begins with "Q" (or
> fullly-uppercase macros and similar things).
>
> I'd really prefer to have it named something like "qtsignature".

A decorator is just a function and needs to reside in a namespace. While I 
could put the signature() function in the global namespace, that would be 
dumb.

> Also, as others said, replicating the whole signal name really stems out as
> unpythonic. Listing the arguments type could be well enough:
>
> @qtsignature("int")
> def on_spinbox_valueChanged(self, value):
>     # ....

The reason for doing it the way I did was to allow the following...

    @QtCore.signature("on_spinbox_valueChanged(int)")
    def handle_int(self, val):
        ...

    @QtCore.signature("on_spinbox_valueChanged(QString)")
    def handle_QString(self, val):
        ...

I can't think of a real-world use case for this - but that's your job, not 
mine.

What I can do is to make the function name optional in the signature, so you 
can use the short version most of the time but you can still have the 
flexibility of the above.

> Actually, you might even use the corresponding Python it instead of a
> string:
>
> @qtsignature(int, QString)
>
> or something like that.

No, you need to express C++ types (where QString is different to QString *) so 
you need a string.

Phil




More information about the PyQt mailing list