[PyKDE] SIP automatically casting up to a QWidget

Phil Thompson phil at riverbankcomputing.co.uk
Sun Feb 25 00:23:51 GMT 2007


On Thursday 22 February 2007 4:33 pm, Matt Newell wrote:
> On Thursday 22 February 2007 06:32, Phil Thompson wrote:
> > On Thursday 22 February 2007 1:53 pm, Paul Giannaros wrote:
> > > I have a class A that i'm wrapping with SIP. The class inherits from a
> > > bunch of other classes. One of the classes it inherits from also in
> > > turn inherits from QWidget. When I import the created module, whenever
> > > I call a method that is meant to return an instance of class A I get a
> > > QWidget instead. I can use sip.cast to get the QWidget back to an A
> > > instance, but that's not a very friendly solution.
> > > Can someone suggest why this is happening, or how to fix it?
> >
> > You need to implement %ConvertToSubClassCode that handles A (and any
> > other QObject derived classes in your module).
>
> While I understand the use of %ConvertToSubClassCode, it seems to me that
> it should not be required for this case.  If the class hierarchy is
>
> QWidget -> A
>
> and the function signature already shows that the function returns class A,
> why should convertSubClass ever convert it to a QWidget?
>
> I changed my sip a while ago for this case, since I have many Dialogs and
> widgets wrapped with sip that I don't really need automatic converters for,
> but don't want them downgraded to a QWidget when they are constructed.
>
> Here's the code I have inside the while loop in convertToSubClass.
>
> /*
> * The base type is the "root" class that may have a number of
> * convertors each handling a "branch" of the derived tree of
> * classes.  The "root" normally implements the base function that
> * provides the RTTI used by the convertors and is re-implemented
> * by derived classes.  We therefore see if the target type is a
> * sub-class of the root, ie. see if the convertor might be able to
> * convert the target type to something more specific.
> */
> if (PyType_IsSubtype((PyTypeObject *)type, (PyTypeObject
> *)scc->scc_basetype)) {
> 	sipWrapperType *subtype;
> 	subtype = (*scc->scc_convertor)(cppPtr);
> 	/*
> 		* Because a base module with convertToSubClass code may know how to
> convert * A -> B -> C
> 		* a derived module may have a class D that inherits from C,
> 		* so the base module may believe it is converting to a more specific type
> 		* by converting to C, when in fact D is already the most specific type.
> 		*/
> 	if (subtype && !PyType_IsSubtype((PyTypeObject*)type,
> (PyTypeObject*)subtype) )
> 		return subtype;
> }

Thanks. The original code was written when a convertor was only supposed to 
recognise exact types (ie. only one convertor would ever return a value).

Phil




More information about the PyQt mailing list