Results 1 to 5 of 5

Thread: make QVector's operator useful in qtscript

  1. #1
    Join Date
    Jun 2010
    Posts
    38
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3

    Default make QVector's operator useful in qtscript

    We can make c++'s Object and Functions available in QtScript,

    Can QVector's operator be availiable in QtScript?

    then in qtscript:
    var vector1 = [1,2,3];
    var vector2 = [3,4,5];
    var a = vector1 + vector2;

    this is what I want get.
    Last edited by wookoon; 17th May 2011 at 11:30.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: make QVector's operator useful in qtscript

    But you are not using QVector here anywhere. There is just javascript code here.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    wookoon (18th May 2011)

  4. #3
    Join Date
    Jun 2010
    Posts
    38
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3

    Default Re: make QVector's operator useful in qtscript

    c++ code:
    Qt Code:
    1. Q_DECLARE_METATYPE(QVector<float>)
    2.  
    3. QVector<float> vector1;
    4. QVector<float> vector2;
    5. QVector<float> vector3;
    6.  
    7. vector1 << 1 << 2 << 3;
    8. vector2 << 3 << 4 << 5;
    9.  
    10. QScriptEngine *engine = new QScriptEngine;
    11.  
    12. qScriptRegisterSequenceMetaType<QVector<float> >(engine);
    13. QScriptValue value = engine->toScriptValue(vector1);
    14. engine->globalObject().setProperty("vec1",value);
    15.  
    16. value = engine->toScriptValue(vector2);
    17. engine->globalObject().setProperty("vec2",value);
    18.  
    19. value = engine->toScriptValue(vector3);
    20. engine->globalObject().setProperty("vec3",value);
    To copy to clipboard, switch view to plain text mode 

    now the vec1,vec2 and vec3 is availiable in Qtscript,

    If I want do like this in QtScript,:

    Qt Code:
    1. vec3 = vec1 + vec2 ,
    To copy to clipboard, switch view to plain text mode 

    Could this be realized in Qtscript?

    I think if can make QVector's operator function be availiable in qtscript, then can realize it.
    Last edited by wookoon; 18th May 2011 at 03:00.

  5. #4
    Join Date
    Jun 2010
    Posts
    38
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3

    Default Re: make QVector's operator useful in qtscript

    Quote Originally Posted by wysota View Post
    But you are not using QVector here anywhere. There is just javascript code here.
    I have try another method:
    I write a object and overide the "+" operator,and make it availiable in qtscript,

    Qt Code:
    1. class MyObject : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit MyObject(QObject *parent = 0);
    6.  
    7. Q_INVOKABLE MyObject* operator+(const MyObject &other)
    8. {
    9. MyObject *object = new MyObject;
    10. object->d = other.d + this->d;
    11. return object;
    12. };
    13.  
    14. void setValue(int value){d = value;};
    15. int value(){return d;};
    16.  
    17. private:
    18. int d;
    19. };
    To copy to clipboard, switch view to plain text mode 

    main.cpp:
    Qt Code:
    1. MyObject * object = new MyObject;
    2. object->setValue(10);
    3. QScriptValue objectValue = engine->newQObject(object);
    4. engine->globalObject().setProperty("obj1", objectValue);
    5.  
    6. MyObject * object2 = new MyObject;
    7. object2->setValue(5);
    8. objectValue = engine->newQObject(object2);
    9. engine->globalObject().setProperty("obj2", objectValue);
    10.  
    11. engine->evaluate("obj1+obj2;");
    To copy to clipboard, switch view to plain text mode 

    But from my debug, the "obj1 + obj2" did not execute the MyObject's "+" operator.

    So this method maybe is no effect.

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: make QVector's operator useful in qtscript

    From what I can see Javascript does not support operator overloading. You need a "named" method for this. You can add this method to your objects by modifying their prototype or defining the method in C++ as part of the class. As for QVector, you need to make it scriptable manually using means such as QScriptable and/or QScriptClass.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. override operator with qtscript
    By wookoon in forum Qt Programming
    Replies: 1
    Last Post: 20th July 2010, 11:29
  2. Calling object's operator+ from QtScript
    By PolyVox in forum Qt Programming
    Replies: 1
    Last Post: 1st July 2010, 23:37
  3. Replies: 0
    Last Post: 25th November 2009, 08:46
  4. Make shared libraries available in QtScript
    By TorAn in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2009, 18:00
  5. Replies: 1
    Last Post: 25th October 2008, 20:18

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
  •  
Qt is a trademark of The Qt Company.