Results 1 to 2 of 2

Thread: QtScript: default constructor question

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

    Default QtScript: default constructor question

    Studying QtScript examples and using my own code (see my previous post regarding QtScript ) I can't quite understand how QtScript instantiate C++ classes where constructor has arguments.

    Tracing the code (shown below) I can see that default constructor with no arguments get called, then my factory code get called and correct test(int) is called with agrument (10, in my case), but at the end of it my script receives an instance constructed with prototype where default constructor is invoked (_i == 0) . So, when following script is evaluated, returned value is 1 and not 11 as I expect.

    Any comments, suggestions are very much appreciated.
    Qt Code:
    1. //Script that is evaluated
    2. var t = new test(10)
    3. t.val + 1
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // part of the code that evaluates script
    2. QString script = scriptText; // above script
    3. QScriptEngine interpreter;
    4. test::registerWithScript("test", interpreter);
    5. QScriptValue result = interpreter.evaluate(script);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. // support class to help with default constructor and registering
    2. template <typename T> class Scriptable
    3. {
    4. public:
    5. Scriptable()
    6. {
    7. }
    8.  
    9.  
    10. static void registerWithScript(const char* scriptObjName, QScriptEngine& scriptengine)
    11. {
    12. T* t = new T();
    13. QScriptValue obj = scriptengine.newQObject(t, QScriptEngine::ScriptOwnership);
    14. scriptengine.setDefaultPrototype(qMetaTypeId<T>(),obj);
    15. QScriptValue objCtor = scriptengine.newFunction(objectConst, obj);
    16. scriptengine.globalObject().setProperty(scriptObjName, objCtor);
    17. }
    18.  
    19. private:
    20. static QScriptValue objectConst (QScriptContext* context, QScriptEngine* scriptengine)
    21. {
    22. boost::scoped_ptr<T> t(T::factory (context));
    23. return scriptengine->toScriptValue(*t);
    24. }
    25. };
    26.  
    27.  
    28. //test class; to study QtScript
    29. class test : public QObject, public QScriptable, public Scriptable<test>
    30. {
    31. Q_OBJECT
    32. Q_PROPERTY(int val READ val WRITE set)
    33. public:
    34. test (int i = 0) : _i(i)
    35. {
    36. }
    37. test (const test& another) : _i(another._i)
    38. {
    39. int i = _i;
    40. }
    41. virtual ~test ()
    42. {
    43. int i = _i;
    44. }
    45.  
    46. test& operator = (const test& another)
    47. {
    48. _i = another._i;
    49. return *this;
    50. }
    51. void set(int i)
    52. {
    53. _i = i;
    54. }
    55. int val()
    56. {
    57. return _i;
    58. }
    59.  
    60. static test* factory (QScriptContext* context)
    61. {
    62. if (context->argumentCount() == 0)
    63. return new test();
    64. else
    65. {
    66. int i = context->argument(0).toInt32();
    67. return new test(i);
    68. }
    69. }
    70. private:
    71. int _i;
    72. };
    73.  
    74. Q_DECLARE_METATYPE(test)
    75. Q_DECLARE_METATYPE(test*)
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QtScript: default constructor question

    I found a solution that works. Actually, by saying "found" in this specific case I meant that I found a discrepancy between "Foundations_of_qt_development" and Qt documentation. Qt wins.

    Qt Code:
    1. //Following are the changes in my support class. It might help someone hitting similar issue. Commented lines are "old version".
    2. template <typename T> class Scriptable
    3. {
    4. public:
    5. Scriptable()
    6. {
    7. }
    8.  
    9.  
    10. static void registerWithScript(const char* scriptObjName, QScriptEngine& scriptengine)
    11. {
    12. T* t = new T();
    13. QScriptValue obj = scriptengine.newQObject(t, QScriptEngine::ScriptOwnership);
    14. scriptengine.setDefaultPrototype(qMetaTypeId<T>(),obj);
    15.  
    16. // QScriptValue objCtor = scriptengine.newFunction(objectConst, obj);
    17. // scriptengine.globalObject().setProperty(scriptObjName, objCtor);
    18.  
    19.  
    20. QScriptValue ctor = scriptengine.newFunction(objectConst);
    21. QScriptValue metaObject = scriptengine.newQMetaObject(&QObject::staticMetaObject, ctor);
    22. scriptengine.globalObject().setProperty(scriptObjName, metaObject);
    23. }
    24.  
    25. private:
    26. static QScriptValue objectConst (QScriptContext* context, QScriptEngine* scriptengine)
    27. {
    28. // boost::scoped_ptr<T> t(T::factory (context));
    29. // return scriptengine->toScriptValue(*t);
    30. T* t = T::factory (context);
    31. return scriptengine->newQObject(t, QScriptEngine::ScriptOwnership);
    32. }
    33. };
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Getting the name of the default style
    By gregsan in forum Qt Programming
    Replies: 4
    Last Post: 28th September 2011, 19:00
  2. QTreeView default drag action
    By onamatic in forum Qt Programming
    Replies: 2
    Last Post: 1st March 2010, 07:37
  3. Replies: 6
    Last Post: 8th July 2009, 20:28
  4. Getting default "system" palette[solved]
    By maverick_pol in forum Qt Programming
    Replies: 0
    Last Post: 2nd April 2008, 17:34

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.