[PyQt] Passing Tuple through SIP...

Phil Thompson phil at riverbankcomputing.com
Mon Nov 2 08:46:03 GMT 2009


On Sun, 1 Nov 2009 14:15:10 -0800, Jamie Riotto <jamie.riotto at gmail.com>
wrote:
> I have a vector class whose .h is:
> 
> class Vec3 {
> public:
> 	float x;
> 	float y;
> 	float z;
> public:
> 	Vec3():x(0.0),y(0.0),z(0.0) {}
> 	Vec3(float X,float Y,float Z):x(X),y(Y),z(Z) {}
> 	Vec3(const Vec3& v) {x=v[0];y=v[1];z=v[2];}
> 	Vec3(const float v[]) {x=v[0];y=v[1];z=v[2];}
> };
> 
> The corresponding SIP file is:
> 
> class Vec3 {
> 
> %TypeHeaderCode
> #include "Vec3.h"
> %End
> public:
> 	float x;
> 	float y;
> 	float z;
> 
> 	Vec3();
> 	Vec3(float x,float y,float z);
> 	Vec3(const Vec3& v2);
> 	//Vec3(const float v2[]);
> };
> 
> 
> Two Issues:
> 1) I can't get the Vec(const float v[]) constructor to compile on SIP.
> I assume that's because I can't pass a pointer from Python?
> So, perhaps the answer is there is no equivalent for this on the
> python side, so leaving this out of the sip file is correct. But then,
> 
> 2) What I really want is to be able to say on the python side is:
> from Vec3 import *
> 
> t = (1,2,3)      # or t = [1,2,3]
> v = Vec3(t)
> 
> I'm confused as to how to go about this, as I can't declare a type
> Tuple in a C++ declaration. Any help would be appreciated.

Specify the Python signature you want (as well as the C++ signature) and
provide code to convert between the two.

In this case the simplest solution is something like...

    Vec3(SIP_PYTUPLE) [(const float *v2)];
%MethodCode
    a0 will be a tuple which you need to convert to an array and call the
ctor
%End

If you want to allow a list as well as a tuple then you will need to do...

    Vec(SIP_PYOBJECT) [(const float *v2)];

...and your conversion code will have to also check that a0 is a sequence
object.

Phil


More information about the PyQt mailing list