[PyQt] SIP template

Phil Thompson phil at riverbankcomputing.com
Wed May 17 08:43:58 BST 2017


On 17 May 2017, at 5:56 am, Denis Rouzaud <denis.rouzaud at gmail.com> wrote:
> 
> Hi Davod,
> 
> Thanks a lot for you reply.
> 
> Le mar. 16 mai 2017 à 23:38, David Boddie <david at boddie.org.uk> a écrit :
> On Tue May 16 20:49:57 BST 2017, Denis Rouzaud wrote:
> 
> > I am facing an issue with correcty implementing a template (container)) in
> > SIP.
> >
> > Let’s say I have
> >
> > qgsoptional:
> > template<class T>
> > class QgsOptional
> > {
> >   public:
> >     QgsOptional( const T &data, bool enabled )
> >       : mEnabled( enabled )
> >       , mData( data )
> >     {}
> >   private:
> >     bool mEnabled;
> >     T mData;
> > };
> >
> > qgsoptionalexpression.h:
> >
> > class QgsOptionalExpression : public QgsOptional<QgsExpression>
> > {
> >   public:
> >     QgsOptionalExpression();
> > }
> 
> OK, so you are defining a class called QgsOptionalExpression here. As far as
> I can see, this will conflict with what you are doing next:
> 
> > Now, I’d like to create the SIP file for QgsOptioalExpression. I tried with
> > what I found in another topic [0]:
> >
> > class QgsOptionalExpression
> > {
> > %TypeHeaderCode
> > #include "qgsoptional.h"
> > typedef QgsOptional<QgsExpression> QgsOptionalExpression;
> > %End
> >   public:
> >     QgsOptionalExpression();
> > }
> 
> Here, you define QgsOptionalExpression again using a typedef, resulting in
> the following error:
> 
> > But this gives me this error:
> > python/core/qgsoptionalexpression.sip:29:36: error: typedef redefinition
> > with different types ('QgsOptional' vs 'QgsOptionalExpression')
> > typedef QgsOptional QgsOptionalExpression;
> > The error points that the definition is already included in the header
> > qgsoptionalepxression.h while I am actually not including it here (also gave
> > it a try with including it).
> >
> > I understand the point, but how shall I tackle this?
> 
> Good. :-) Just for others reading this, the reason for the conflict is that
> the typedef is included in the generated C++ code, so the compiler will
> encounter both the class definition and the typedef.
> 
> If you don't need the class definition then remove it.
> 
> If you do need it then I think you should be able to leave out the typedef
> and include the custom qgsoptionalexpression.h header instead:
> 
> class QgsOptionalExpression
> {
> %TypeHeaderCode
> #include "qgsoptionalexpression.h"
> %End
>   public:
>     QgsOptionalExpression();
> }
> 
> Does that help?
> 
> 
> This compiles but I don't have access to enabled() and data() methods in the template class (QgsOptional), which is a major issue.
> 
> With the inheritance written: 
> class QgsOptionalExpression : public QgOptional<QgsExpression> 
> I get a "syntax error".
> 
> Any idea?

Just lie to SIP and add them to the definition of QgsOptionalExpression.

See QItemSelection in qitemselectionmodel.sip in PyQt5.

Phil


More information about the PyQt mailing list