Results 1 to 2 of 2

Thread: How to use c++ complex numbers in QtScript

  1. #1
    Join Date
    Apr 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default How to use c++ complex numbers in QtScript

    Hi,

    I have a class relying totally on the use of complex numbers. I'm using the c++ standard complex class for this.
    Making this class scriptable, following the examples given in http://doc.qt.nokia.com/4.6/scripting.html wasn't a big deal.
    Now I'm able to call all routines using real (double) arguments. However I have absolutely no clue how to call routines using complex arguments. Doe anyone out there know how to do it?

    Best,
    FFox

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use c++ complex numbers in QtScript

    Hi there!

    I never used c++ complex numbers before. But you will need to register the datatype to the scripting engine. One way to do that is to use qScriptRegisterMetaType. But that doesn't work for types that are not supported by QScriptValue ..

    Qt Code:
    1. QScriptValue TypeToScriptValue(QScriptEngine *engine, Type* const &in)
    2. {
    3. return engine->newQObject(in,QScriptEngine::QtOwnership,QScriptEngine::AutoCreateDynamicProperties);
    4. }
    5. void TypeFromScriptValue(const QScriptValue &object, Type* &out)
    6. {
    7. out = qobject_cast<Type*>(object.toQObject());
    8. }
    9.  
    10. qScriptRegisterMetaType<Type*>(scriptengine, TypeToScriptValue, TypeFromScriptValue);
    To copy to clipboard, switch view to plain text mode 
    For a simple struct, that I think the c++ complex number will be similar too, you could create a prototype as follows:


    Qt Code:
    1. template <typename T>
    2. struct CVec4
    3. {
    4. T vec[4];
    5. T x() const {return vec[0];}
    6. T y() const {return vec[1];}
    7. T z() const {return vec[2];}
    8. T a() const {return vec[3];}
    9. void setX(const T c) {vec[0] = c;}
    10. void setY(const T c) {vec[1] = c;}
    11. void setZ(const T c) {vec[2] = c;}
    12. void setA(const T c) {vec[3] = c;}
    13.  
    14. CVec4(T _x = 0,T _y = 0,T _z = 0,T _a = 1) {vec[0] = _x;vec[1] = _y;vec[2] = _z;vec[3] = _a;}
    15. CVec4(const CVec3<f32>& b) {vec[0] = b.x();vec[1] = b.y();vec[2] = b.z();vec[3] = 1;}
    16. CVec4(const CVec3<f64>& b) {vec[0] = b.x();vec[1] = b.y();vec[2] = b.z();vec[3] = 1;}
    17.  
    18. const CVec4 operator+(const CVec4& b) const {return CVec4(x()+b.x(),y()+b.y(),z()+b.z(),a()+b.a());}
    19. const CVec4 operator-(const CVec4& b) const {return CVec4(x()-b.x(),y()-b.y(),z()-b.z(),a()-b.a());}
    20.  
    21. CVec4& operator=(const CVec4& b) {vec[0] = b.x();vec[1] = b.y();vec[2] = b.z();vec[3] = b.a();return *this;}
    22. CVec4& operator*=(const T s) {vec[0] *= s;vec[1] *= s;vec[2] *= s;return *this;}
    23.  
    24. CVec4& operator+=(const CVec4& b) {*this = *this + b;return *this;}
    25.  
    26. const CVec4 operator*(const T s) const {CVec4 a = *this;a *= s;return a;}
    27.  
    28. CVec4 MultMatrix(const f32* mat) const
    29. {
    30. return CVec4(vec[0] * mat[0]+vec[1] * mat[4]+vec[2] * mat[8]+vec[3] * mat[12],
    31. vec[0] * mat[1]+vec[1] * mat[5]+vec[2] * mat[9]+vec[3] * mat[13],
    32. vec[0] * mat[2]+vec[1] * mat[6]+vec[2] * mat[10]+vec[3] * mat[14],
    33. vec[0] * mat[3]+vec[1] * mat[7]+vec[2] * mat[11]+vec[3] * mat[15]);
    34. }
    35.  
    36. CVec4<f64> Getf64(const f64 s) const {CVec4<f64> a(ex4(*this));a *= s;return a;}
    37.  
    38. QString toString() const {return "("+QString::number(x())+";"+QString::number(y())+";"+QString::number(z())+";"+QString::number(a())+")";}
    39. };
    40.  
    41. typedef CVec4<f32> CVec4f;
    42.  
    43. typedef CVec4f CGL4f;
    44.  
    45. Q_DECLARE_METATYPE(CGL4f)
    46. Q_DECLARE_METATYPE(CGL4f*)
    47. Q_DECLARE_METATYPE(QList<CGL4f>)
    48.  
    49. class CGL4fPrototype : public QObject, public QScriptable
    50. {
    51. Q_OBJECT
    52. Q_PROPERTY(qreal x READ X WRITE setX)
    53. Q_PROPERTY(qreal y READ Y WRITE setY)
    54. Q_PROPERTY(qreal z READ Z WRITE setZ)
    55. Q_PROPERTY(qreal a READ A WRITE setA)
    56. protected:
    57. CGL4f* thisCGL4() const {return qscriptvalue_cast<CGL4f*>(thisObject());}
    58.  
    59. public:
    60. CGL4fPrototype(QObject *parent = 0) : QObject(parent) {}
    61.  
    62. qreal X() const {return thisCGL4()->x();}
    63. void setX(const qreal &x) {thisCGL4()->setX(x);}
    64.  
    65. qreal Y() const {return thisCGL4()->y();}
    66. void setY(const qreal &y) {thisCGL4()->setY(y);}
    67.  
    68. qreal Z() const {return thisCGL4()->z();}
    69. void setZ(const qreal &z) {thisCGL4()->setZ(z);}
    70.  
    71. qreal A() const {return thisCGL4()->a();}
    72. void setA(const qreal &a) {thisCGL4()->setA(a);}
    73.  
    74. public slots:
    75. QString toString() const {return thisCGL4()->toString();}
    76. };
    77.  
    78. QScriptValue CGL4f_ctor(QScriptContext *context, QScriptEngine *engine)
    79. {
    80. if (context->argumentCount() >= 3)
    81. {
    82. qreal x = context->argument(0).toNumber();
    83. qreal y = context->argument(1).toNumber();
    84. qreal z = context->argument(2).toNumber();
    85. qreal a = 1.0;
    86. if (context->argumentCount() > 3)
    87. context->argument(3).toNumber();
    88. return engine->toScriptValue(CGL4f(x, y, z, a));
    89. }
    90. else {
    91. return context->throwError(QScriptContext::SyntaxError, "CGL4f constructor requires 3/4 parameters (x,y,z,a/1)!");
    92. }
    93. }
    94.  
    95. CGL4fPrototype* cgl4fProto = new CGL4fPrototype();
    96. scripting->setDefaultPrototype(qMetaTypeId<CGL4f>(),scripting->newQObject(cgl4fProto));
    97. scripting->setDefaultPrototype(qMetaTypeId<CGL4f*>(),scripting->newQObject(cgl4fProto));
    98. scripting->globalObject().setProperty("CGL4f", scripting->newFunction(CGL4f_ctor));
    99. qScriptRegisterSequenceMetaType<QList<CGL4f> >(scripting);
    To copy to clipboard, switch view to plain text mode 

    Afterwards you can call something like this in script:

    Qt Code:
    1. object.setColor(new CGL4f(1,1,0.5));
    To copy to clipboard, switch view to plain text mode 

    where setColor takes a CGL4f argument..

    Hope the amount of code doesn't frighten you. It's a complete example of a 4dimensional vector struct made available to scripting by hand :->

    HIH

    Johannes
    Last edited by JohannesMunk; 6th July 2010 at 16:58.

Similar Threads

  1. Replies: 1
    Last Post: 7th December 2009, 15:59
  2. Replies: 0
    Last Post: 25th November 2009, 08:46
  3. Complex number
    By nbo10 in forum Qt Programming
    Replies: 1
    Last Post: 4th April 2009, 07:44
  4. Complex QCompleter
    By Amouse in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2008, 23:39
  5. Complex Numbers
    By Max Yaffe in forum Qt Programming
    Replies: 2
    Last Post: 24th May 2007, 18:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.