Results 1 to 3 of 3

Thread: QtScript how to handle custom classes derived from QObject

  1. #1
    Join Date
    Jul 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QtScript how to handle custom classes derived from QObject

    Hi,

    I wrote two C++ classes in which I defined a point and a line that derive from qObject in order to make them scriptable using QtScript. I also wrote a simple form in which I would like to create istances of points and lines and use their methods to create more complex entities.

    I declared these objects in this way:

    Qt Code:
    1. Q_SCRIPT_DECLARE_QMETAOBJECT(point, QObject*);
    2. Q_SCRIPT_DECLARE_QMETAOBJECT(line, QObject*);
    To copy to clipboard, switch view to plain text mode 


    Then I created a QScriptEngine in my form constructor and I linked the two classes to the engine in this way:


    Qt Code:
    1. engine->globalObject().setProperty("point",engine->scriptValueFromQMetaObject<point>;());
    2. engine->globalObject().setProperty("line",engine->scriptValueFromQMetaObject<line>;());
    To copy to clipboard, switch view to plain text mode 


    Now I can create for example a point in my form and use its methods without any problem:


    Qt Code:
    1. P0=new point();
    2. P0.getX();
    To copy to clipboard, switch view to plain text mode 


    The problems come when I want to use line's methods that give as a return type a point object:

    --- line.h ---
    Qt Code:
    1. class line : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. line(QObject* parent=0);
    6. //! Line object destructor.
    7. ~line();
    8. Q_INVOKABLE point getLineVertex(float);
    9.  
    10. private:
    11. point P0_;
    12. point P1_;
    13. float a_;
    14. float b_;
    15. float c_;
    16. };
    To copy to clipboard, switch view to plain text mode 


    ---line.cpp---
    Qt Code:
    1. line::line(QObject * parent):QObject(parent){
    2. P0_.setX(0); P0_.setY(0); P0_.setZ(0);
    3. P1_.setX(1.0); P1_.setY(1.0); P1_.setZ(1.0);
    4. a_=P1_.getX()-P1_.getX(); //If using the diff operation
    5. b_=P1_.getY()-P1_.getY(); //it will generate a new Point
    6. c_=P1_.getZ()-P1_.getZ();
    7. }
    8.  
    9. point line::getLineVertex(float t_){
    10. point P(P0_.getX()+a_*t_, P0_.getY()+b_*t_, P0_.getZ()+c_*t_);
    11. return P;
    12. }
    To copy to clipboard, switch view to plain text mode 


    The program complains asking to register the 'point' type with qScriptRegisterMetaType() function. After googling for a day I really haven't find a solution to this issue (it seems that I have to define two functions to convert the class type to the QScriptValue type but these solutions work only on pointers and I prefer to avoid them)....
    I'm kindly asking you if is there someone that can help me in some way :-)!
    Thank you very much
    Norman
    Last edited by Norman; 28th July 2009 at 10:35.

  2. #2
    Join Date
    Jul 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QtScript how to handle custom classes derived from QObject

    I have solved it by myself

    Thank you

    Norman

  3. #3
    Join Date
    Apr 2011
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QtScript how to handle custom classes derived from QObject

    Quote Originally Posted by Norman View Post
    I have solved it by myself

    Thank you

    Norman
    Even if your thread is very old, it is always helpful to publish your solution.

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.