[PyQt] downcast a base class to its template subclass

Phil Thompson phil at riverbankcomputing.com
Tue Nov 2 08:32:21 GMT 2010


On Mon, 1 Nov 2010 17:26:41 -0700, Qin Shen <jeanshen at tippett.com> wrote:
> Hi,
> 
> In my non-template base class' .sip file, I used %ConvertToSubClassCode 
> directive
> to downcast the base class type to its sub-class. If the sub-class is 
> not a template,
> it all works fine. Now I need to derive a template sub-class from the 
> base class,
> the sipFindType() won't work because there isn't any Python class 
> implemented
> for the templated sub-class. What should I do in this case?
> 
> 
> The files look like this:
> 
> In [BaseEntity.sip]
> --------------------------------------------
> class BaseEntity
> {
> %TypeHeaderCode
>     #include <BaseEntity.h>
> %End
> 
> %ConvertToSubClassCode
>     sipType = sipFindType(sipCpp->classType());
> %End
> 
> public:
>     ....
> 
> }
> 
> 
> 
> [CustomEntity.h]
> -------------------------------------------
> template <int ID>
> class CustomEntity : BaseEntity
> {
> public:
>     CustomEntity();
>     virtual ~CustomEntity();
> 
>     std::string classType() const
>     {
>         std::ostringstream nameStream;
>         nameStream << "CustomEntity" << std::setfill('0') << 
> std::setw(2) << ID;
>         return nameStream.str();
>     }
> 
>     ... // more stuff here
> }
> 
> 
> [CustomEntity.sip]
> -------------------------------------------
> template <ID>
> class CustomEntity : BaseEntity
> {
> %TypeHeaderCode
>     #include <CustomEntity.h>
> %End
> 
> public:
>     CustomEntity();
>     virtual ~CustomEntity();
> 
>     std::string classType() const;
> 
>     ... // more stuff here
> }

So how is the template sub-class implemented in Python? Your
%ConvertToSubClassCode will need to explicitly check for the template
sub-class and return the appropriate Python type.

Phil


More information about the PyQt mailing list