[PyKDE] Auto-connecting Slots

Patrick K. O'Brien pobrien at orbtech.com
Sat Jan 28 00:00:52 GMT 2006


Andreas Pakulat wrote:
> On 27.01.06 22:46:29, Phil Thompson wrote:
>> On Friday 27 January 2006 8:10 pm, Patrick K. O'Brien wrote:
>>> Phil Thompson wrote:
>>>> Tonight's PyQt4 snapshot implements Torsten's suggestion for using a
>>>> decorator to control which signal/slot connections are made when pyuic4
>>>> auto-connects.
>>>>
>>>> The problem is best demonstrated by QSpinBox which emits
>>>> valueChanged(QString) and valueChanged(int). Defining an auto-connect
>>>> method
>>>> on_spinbox_valueChanged means that the method is connected to both
>>>> signals and so gets invoked twice. Until now the method had to look at
>>>> the type of the argument and just return if it was the one it didn't
>>>> want.
>>>>
>>>> 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.
>>> Any chance we could get an easier syntax, such as:
>>>
>>>     @qtsig(int)
>>>     def on_spinbox_valueChanged(self, value):
>> I want to keep it generic in case some other uses pop up.
>>
>> At the very least your example would have to be @QtCore.qtsig("int")
> 
> Now I know near to nothing about signatures thus I have to ask:

I think you mean decorators.

> The signature would always sit right in front of the function
> definition? 

Yes.

> If yes than I guess the short version might be ok when it is documented
> good (not just a comment with some example, at least when the first
> version is released). 
> 
> If you can put the signature somewhere else (which I doubt currently),

No, you can't.

> then the longer version would IMHO be better as you can directly see
> which slot is meant. 

Anything longer than necessary is redundant, imo.

> BTW: Will this also solve "issues" like the clicked()-signal from
> QAbstractButton? So I could do 
> @QtCore.signature("on_mybutton_clicked()")
> def on_mybutton_clicked(self):
> and I would get only the call when checked is None and not the 2nd?

While I think that particular form of the decorator is butt-ugly,
something like it should definitely be able to deal with all the
variations that Qt likes to support.

-- 
Patrick K. O'Brien
Orbtech       http://www.orbtech.com
Schevo        http://www.schevo.org
Louie         http://louie.berlios.de




More information about the PyQt mailing list