[PyKDE] Qt progress example with minor glitch

Phil Thompson phil at river-bank.demon.co.uk
Fri Aug 30 20:35:01 BST 2002


Hans-Peter Jansen wrote:

> On Friday 30 August 2002 17:38, Phil Thompson wrote:
> 
>>Hans-Peter Jansen wrote:
>>
>>>Also I forgot to mention in my former post, that QPainter.fillRect
>>>doesn't allow a QColor instance as 5th parameter, unlike Qt. One has
>>>always to wrap QColor with QBrush in PyQt. Phil?
>>>
>>SIP generated code doesn't support the C++ feature of knowing it can
>>call QBrush(QColor) when it needs a QBrush but is given a QColor. It's
>>been on the TODO list for a long time - I may get around to it one day,
>>but I think there are some "issues".
>>
>>Phil
>>
> 
> I see. BTW, how do you think about supporting pythons special methods?
> That is one of those things in the wxPython binding, I liked a lot.
> Look, how Robin got around this (with a patched SWIG, though...):
> 
> class wxPointPtr :
>     def __init__(self,this):
>         self.this = this
>         self.thisown = 0
>     def __del__(self,miscc=miscc):
>         if self.thisown == 1 :
>             miscc.delete_wxPoint(self)
>     def Set(self, *_args, **_kwargs):
>         val = apply(miscc.wxPoint_Set,(self,) + _args, _kwargs)
>         return val
>     def asTuple(self, *_args, **_kwargs):
>         val = apply(miscc.wxPoint_asTuple,(self,) + _args, _kwargs)
>         return val
>     def __add__(self, *_args, **_kwargs):
>         val = apply(miscc.wxPoint___add__,(self,) + _args, _kwargs)
>         if val: val = wxPointPtr(val) ; val.thisown = 1
>         return val
>     def __sub__(self, *_args, **_kwargs):
>         val = apply(miscc.wxPoint___sub__,(self,) + _args, _kwargs)
>         if val: val = wxPointPtr(val) ; val.thisown = 1
>         return val
>     def __cmp__(self, *_args, **_kwargs):
>         val = apply(miscc.wxPoint___cmp__,(self,) + _args, _kwargs)
>         return val
>     def __setattr__(self,name,value):
>         if name == "x" :
>             miscc.wxPoint_x_set(self,value)
>             return
>         if name == "y" :
>             miscc.wxPoint_y_set(self,value)
>             return
>         self.__dict__[name] = value
>     def __getattr__(self,name):
>         if name == "x" :·
>             return miscc.wxPoint_x_get(self)
>         if name == "y" :·
>             return miscc.wxPoint_y_get(self)
>         raise AttributeError,name
>     def __repr__(self):
>         return "<C wxPoint instance at %s>" % (self.this,)
> 
>     def __str__(self):                   return str(self.asTuple())
>     def __repr__(self):                  return str(self.asTuple())
>     def __len__(self):                   return len(self.asTuple())
>     def __getitem__(self, index):        return self.asTuple()[index]
>     def __setitem__(self, index, val):
>         if index == 0: self.x = val
>         elif index == 1: self.y = val
>         else: raise IndexError
> 
> class wxPoint(wxPointPtr):
>     def __init__(self,*_args,**_kwargs):
>         self.this = apply(miscc.new_wxPoint,_args,_kwargs)
>         self.thisown = 1
> 
> Here is the corresponding SWIG source:
> 
> class wxPoint {
> public:
>     long x;
>     long y;
>     wxPoint(long x=0, long y=0);
>     ~wxPoint();
> 
>     %addmethods {
>         void Set(long x, long y) {
>             self->x = x;
>             self->y = y;
>         }
>         PyObject* asTuple() {
>             PyObject* tup = PyTuple_New(2);
>             PyTuple_SET_ITEM(tup, 0, PyInt_FromLong(self->x));
>             PyTuple_SET_ITEM(tup, 1, PyInt_FromLong(self->y));
>             return tup;
>         }
> 
>         wxPoint __add__(const wxPoint* p) {
>             if (! p) return *self;
>             return *self + *p;
>         }
> 
>         wxPoint __sub__(const wxPoint* p) {
>             if (! p) return *self;
>             return *self - *p;
>         }
> 
>         int __cmp__(const wxPoint* p) {
>             if (! p) return 1;
>             if (*self == *p) return 0;
>             return -1;
>         }
>     }
>     %pragma(python) addtoclass = "
>     def __str__(self):                   return str(self.asTuple())
>     def __repr__(self):                  return str(self.asTuple())
>     def __len__(self):                   return len(self.asTuple())
>     def __getitem__(self, index):        return self.asTuple()[index]
>     def __setitem__(self, index, val):
>         if index == 0: self.x = val
>         elif index == 1: self.y = val
>         else: raise IndexError
> "
> };
> 
> Are there any concerns besides missing time not to take this route 
> to get more pythonic one day?


SIP already supports them and they have been implemented in PyQt where a 
class would be useless without them - see QStringList.

They could be exploited much more in PyQt than they are - I've just 
never got around to it.

Phil




More information about the PyQt mailing list