[PyKDE] How to do operators with SIP

Phil Thompson phil at river-bank.demon.co.uk
Sat Nov 17 08:08:17 GMT 2001


"Ewald R. de Wit" wrote:
> 
> Hi,
> 
> Does anyone here know how to convert C++ operators like +, *, / etc
> using SIP? Suppose I have a class called Matrix that has + defined for
> it in C++, what would the SIP code look like?
> 
> Supposedly something like this:
> 
> ------------------------
> PyNumberMethods:
>         __add__
> %MemberCode
> 
>         ... stuff ...
> 
> %End
> ------------------------
> 
> but I can't find out how to do the 'stuff' part.

Have a look at how qstringlist.sip implements __contains__, __getitem__
and __len__.

Note that the SIP syntax has changed slightly in v3.0.

Implementing the '+' operator for QPoint would look something like...

PyNumberMethod __add__
%MemberCode
    QPoint *p0, *p1;

    if ((p0 = (QPoint *)sipGetCppPtr((sipThisType *)a0,sipClass_QPoint))
== NULL)
        return NULL;

    if ((p1 = (QPoint *)sipGetCppPtr((sipThisType *)a1,sipClass_QPoint))
== NULL)
        return NULL;

    return sipNewCppToSelf(new QPoint(*p0 +
*p1),sipClass_QPoint,SIP_PY_OWNED|SIP_SIMPLE);
%End

Phil




More information about the PyQt mailing list