Results 1 to 1 of 1

Thread: QtScript evaluation question

  1. #1
    Join Date
    Jul 2007
    Posts
    121
    Thanks
    38
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QtScript evaluation question

    I started to learn QtScript, creating following classes:

    Qt Code:
    1. template <typename T> class Scriptable
    2. {
    3. public:
    4. Scriptable() {}
    5. static void registerWithScript(const char* scriptObjName, QScriptEngine& scriptengine)
    6. {
    7. T* t = new T();
    8. QScriptValue obj = scriptengine.newQObject(t, QScriptEngine::ScriptOwnership);
    9. scriptengine.setDefaultPrototype(qMetaTypeId<T>(),obj);
    10.  
    11. QScriptValue testCtor = scriptengine.newFunction(testConstructor, obj);
    12. scriptengine.globalObject().setProperty(scriptObjName, testCtor);
    13. }
    14. private:
    15. static QScriptValue testConstructor(QScriptContext* /* context */,
    16. QScriptEngine *interpreter)
    17. {
    18. return interpreter->toScriptValue(T());
    19. }
    20. };
    21.  
    22.  
    23. class test : public QObject, public QScriptable, public Scriptable<test>
    24. {
    25. Q_OBJECT
    26. Q_PROPERTY(int val READ val WRITE set)
    27. public:
    28. test (int i = 0) : _i(i) { }
    29. test (const test& another) : _i(another._i){}
    30. virtual ~test () {}
    31. void set(int i)
    32. {
    33. _i = i;
    34. }
    35. int val()
    36. {
    37. return _i;
    38. }
    39.  
    40. private:
    41. int _i;
    42. };
    To copy to clipboard, switch view to plain text mode 
    Q_DECLARE_METATYPE(test)

    Then I wrote two short scripts:
    Script A:
    Qt Code:
    1. var t = new test()
    2. t.val = 15
    3. t.val = t.val + 7
    To copy to clipboard, switch view to plain text mode 

    and Script B
    Qt Code:
    1. var t = new test()
    2. t.val = 15
    3. var m = new test()
    4. m.val = 20
    5. t.val + m.val
    To copy to clipboard, switch view to plain text mode 
    I run into following problem:
    When I execute the script A – everything works fine, script returns 22 as I expect.
    But when I execute script B it retuns 40, instead of expected 35. It appears as if variable “t” is replaced with “m” and therefore it returns double of whatever I assign to m.val and "t" is lost.

    Below is what I do to run the script:
    Qt Code:
    1. QString script = ui.txtRequest->toPlainText(); // script text
    2. QScriptEngine interpreter;
    3. test::registerWithScript("test", interpreter);
    4. QScriptValue result = interpreter.evaluate(script);
    To copy to clipboard, switch view to plain text mode 

    Any advice is greatly appreciated
    Last edited by QPlace; 22nd October 2009 at 03:52.

Similar Threads

  1. QTScript - checking variable names?
    By android_ in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2009, 18:40
  2. QtScript Binding problem with QWidget
    By zack in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2009, 09:38
  3. Basic QtScript question
    By jimboqt in forum Newbie
    Replies: 0
    Last Post: 23rd September 2008, 14:09
  4. using the qt 4.4.0 evaluation version with MinGw
    By dano in forum Installation and Deployment
    Replies: 4
    Last Post: 4th July 2008, 16:02

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.