Results 1 to 10 of 10

Thread: QtScript: accessing methods of QObject* subclass from script

  1. #1
    Join Date
    Mar 2008
    Posts
    27
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QtScript: accessing methods of QObject* subclass from script

    I am very new to QtScript and I have used QScriptEngine::evaluate() to evaluate this script:

    Qt Code:
    1. function render(node) {
    2. var stream = node.getStream();
    3. print("typeof stream "+typeof stream + "," + stream);
    4. var test = stream.test();
    5. }
    To copy to clipboard, switch view to plain text mode 

    Now, I have used Q_DECLARE_METATYPE(Stream*)
    and node.getStream() returns Stream*

    In the script the output is as follows:

    Qt Code:
    1. typeof stream object,QVariant(Stream*)
    To copy to clipboard, switch view to plain text mode 

    Stream has a public slot with testmethod() returning an int:

    Qt Code:
    1. public slots:
    2. int testmethod() {return 8;}
    To copy to clipboard, switch view to plain text mode 

    But then I get the following TypeError in the script when I try to access the testmethod() method of Stream

    TypeError: Result of expression 'stream.testmethod' [undefined] is not a function.

    I am obviously not doing this right? Can someone help me in the right direction to make this work?

    Many thanks.

  2. #2
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtScript: accessing methods of QObject* subclass from script

    Bump, same problem.

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

    Default Re: QtScript: accessing methods of QObject* subclass from script

    The object needs to be exposed as QObject and not QVariant.
    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.


  4. #4
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtScript: accessing methods of QObject* subclass from script

    Ok, mine is not exactly the same problem.

    I had exposed a instance of a class that inherits QObject, ClsUserManager:

    Qt Code:
    1. engine.globalObject().setProperty("UserManager",engine.newQObject(UserManager));
    To copy to clipboard, switch view to plain text mode 

    UserManager has this public slot:
    Qt Code:
    1. OtherCustomClass* getUser(QString id);
    To copy to clipboard, switch view to plain text mode 

    OtherCustomClass has a slot:
    Qt Code:
    1. void setProperty(QString p)
    To copy to clipboard, switch view to plain text mode 

    I tried to call de slot from my script:
    Qt Code:
    1. UserManager.getUser(id).setProperty(p);
    To copy to clipboard, switch view to plain text mode 

    I get:
    Qt Code:
    1. Debug: "TypeError: cannot call getUser(): unknown return type `OtherCustomClass*' (register the type with qScriptRegisterMetaType())"
    To copy to clipboard, switch view to plain text mode 

    The problem is that I can't call qScriptRegisterMetaType() from OtherCustomClass, it gets "QScriptEngine * engine" as first param, and I created some instances of QScriptEngine (by demmand) dinamically.

    Regards

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

    Default Re: QtScript: accessing methods of QObject* subclass from script

    Does "OtherCustomClass" inherit QObject?
    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.


  6. #6
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtScript: accessing methods of QObject* subclass from script

    Yes, all classes I use in the project inherit QObject.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QtScript: accessing methods of QObject* subclass from script

    Does it have a Q_OBJECT macro?
    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.


  8. #8
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtScript: accessing methods of QObject* subclass from script

    Yes, I can call their slots from C++ code without any problem. I could try to reproduce it on a minimal compilable project if you need it.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QtScript: accessing methods of QObject* subclass from script

    Ok, got it...

    The problem is that you need to teach your script engine to convert your objects to script values.

    Here is the relevant code:

    Qt Code:
    1. Q_DECLARE_METATYPE(OtherCustomClass*)
    2.  
    3. QScriptValue toScriptValue(QScriptEngine *e, OtherCustomClass* const &o) {
    4. return e->newQObject(o);
    5. }
    6.  
    7. void fromScriptValue(const QScriptValue &val, OtherCustomClass* &o) {
    8. o = qobject_cast<OtherCustomClass*>(val.toQObject());
    9. }
    10.  
    11. qScriptRegisterMetaType<OtherCustomClass*>(engine, fromScriptValue, toScriptValue);
    To copy to clipboard, switch view to plain text mode 

    Or more general:

    Qt Code:
    1. template <typename Tp>
    2. QScriptValue qScriptValueFromQObject(QScriptEngine *engine, Tp const
    3. &qobject)
    4. {
    5. return engine->newQObject(qobject);
    6. }
    7.  
    8. template <typename Tp>
    9. void qScriptValueToQObject(const QScriptValue &value, Tp &qobject)
    10. {
    11. qobject = qobject_cast<Tp>(value.toQObject());
    12. }
    13.  
    14. template <typename Tp>
    15. int qScriptRegisterQObjectMetaType(
    16. QScriptEngine *engine,
    17. const QScriptValue &prototype = QScriptValue()
    18. #ifndef qdoc
    19. , Tp * /* dummy */ = 0
    20. #endif
    21. )
    22. {
    23. return qScriptRegisterMetaType<Tp>(engine, qScriptValueFromQObject, qScriptValueToQObject, prototype);
    24. }
    25.  
    26. qScriptRegisterQObjectMetaType<OtherCustomClass*>(engine);
    To copy to clipboard, switch view to plain text mode 

    The last snippet is not my invention, borrowed it from here: http://lists.trolltech.com/qt-intere...ad00158-0.html
    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.


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

    [Zero] (5th July 2012)

  11. #10
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtScript: accessing methods of QObject* subclass from script

    Solved, Thank you very much!

Similar Threads

  1. Dynamic plugin: accessing parent's methods
    By codeslicer in forum Qt Programming
    Replies: 1
    Last Post: 4th January 2011, 10:16
  2. QtScript - How to use maths functions in a script?
    By behu in forum Qt Programming
    Replies: 2
    Last Post: 18th October 2010, 09:57
  3. QtScript. How to add created object to script?
    By sergey_85 in forum Qt Programming
    Replies: 1
    Last Post: 18th February 2010, 15:24
  4. QtScript: how to reload current script?
    By eyeofhell in forum Qt Programming
    Replies: 7
    Last Post: 12th January 2010, 11:28
  5. Accessing QMainWindow methods
    By vieraci in forum Qt Programming
    Replies: 2
    Last Post: 21st April 2009, 09:38

Tags for this Thread

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.