[PyQt] sip %ConvertToSubClassCode problem with %TypeHeaderCode doesn't work as expected.

Blaine Bell blaine.bell at schrodinger.com
Wed Jun 2 22:20:26 BST 2010


I am new to using SIP, and I find it *very* difficult to find reasonable 
documentation and examples to do what I need to do.  I want to be able 
to have SIP automatically cast my instances to a subclass.  I am using 
"%ConvertToSubClassCode" that uses dynamic_cast<> to figure out whether 
the sipCpp is my class, and if it is, then I convert it to the related 
sip class.  Here is the snippet of my code:
~~~~~~~~~~~~~~~
class ChmMainWindow : ChmBaseView
{
%TypeHeaderCode
#include "chmmainwindow.h"
%End
%ConvertToSubClassCode
    ChmMainWindow *mw = dynamic_cast<ChmMainWindow *>(sipCpp);
    if (mw){
        sipClass = sipAPI_canvasapp->api_find_class("ChmMainWindow");
    } else {
        sipClass = NULL;
    }
%End
~~~~~~~~~~~~~~~
However, the compiler fails to recognize the "ChmMainWindow" class when 
compiling the sipcanvasappcmodule.cpp file because the %TypeHeaderCode 
does not get put into that file before this code. (i.e., this code only 
gets put into the actual SIP wrapper cpp file).  My workaround is to put 
this "#include "chmmainwindow.h" into a different %TypeHeaderCode block 
further down the SIP file, and this seems to work fine (I am not sure 
why some %TypeHeaderCode blocks get put into the cmodule and some others 
do not).

Also, I am not sure why (forgive my ignorance) this isn't the default 
behavior?  Why shouldn't subclasses implemented in SIP get converted by 
default?  I see the Qt code implements all of the casting in one object 
(i.e., QApplication), is it a performance issue?

Thanks a lot for your response in advance.

Blaine


More information about the PyQt mailing list