Results 1 to 6 of 6

Thread: [Advanced] Qt Script access QObject from C++ code

  1. #1
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Lightbulb [Advanced] Qt Script access QObject from C++ code

    Hey guys,

    I'm using the Qt script bindings from Qt Labs.

    I'm writing the following lines in my script :

    Qt Code:
    1. function main() {}
    2.  
    3. main.prototype.createPage = function()
    4. {
    5. box = new QWidget; // I need that QWidget pointer in my C++ code
    6. }
    To copy to clipboard, switch view to plain text mode 

    I'm calling this function from C++ code.
    Afterward, I need to gather all references to Qt Objects.

    How can I do that ?

    Thanks.

  2. #2
    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: [Advanced] Qt Script access QObject from C++ code

    You can have a function in script that keeps references to all objects created and you can assign a function to the global object of the engine that will return a list of them. Then it's just a matter of calling that function using QScriptEngine::evaluate() and interpreting the resulting QScriptValue as a list of objects.
    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:

    bunjee (20th May 2009)

  4. #3
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: [Advanced] Qt Script access QObject from C++ code

    Quote Originally Posted by wysota View Post
    You can have a function in script that keeps references to all objects created.
    Sounds good, how can I do that ? (I want this to be transparent)

  5. #4
    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: [Advanced] Qt Script access QObject from C++ code

    Call it from the function you pasted in your previous post just before returning the object or modify the prototype of each class you want managed directly.
    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. The following user says thank you to wysota for this useful post:

    bunjee (21st May 2009)

  7. #5
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Thumbs up Re: [Advanced] Qt Script access QObject from C++ code

    I did it this way:

    Qt script code:
    Qt Code:
    1. function New(This, object)
    2. {
    3. if (!This.objects) This.objects = new Array;
    4.  
    5. This.objects.push(object);
    6. return object;
    7. }
    8.  
    9. function objects() { return this.objects; } // Getting objects from C++
    10.  
    11. ...
    12. box = New(this, new QWidget);
    13. ...
    To copy to clipboard, switch view to plain text mode 

    C++ code:

    Qt Code:
    1. void qkScriptPrivate::updateObjects()
    2. {
    3. Q_Q(qkScript);
    4.  
    5. QScriptValue objects = engine->globalObject().property("objects");
    6. QScriptValue output = objects.call(scriptValue);
    7.  
    8. QScriptValueIterator it(output);
    9. while (it.hasNext())
    10. {
    11. it.next();
    12.  
    13. if (it.value().isQObject()) q->declareObject(it.value().toQObject());
    14. qkPrint("%s : %s", it.name().C_STR, it.value().toString().C_STR);
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    Note that you can make it fully transparent by parsing every "new myObject" in the script and replace it by "New(this, new myObject)".

  8. #6
    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: [Advanced] Qt Script access QObject from C++ code

    Great, that's exactly what I meant
    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. Structure of the code with database access
    By PaceyIV in forum Newbie
    Replies: 0
    Last Post: 31st March 2009, 19:12
  2. problem with linking
    By mickey in forum Qt Programming
    Replies: 49
    Last Post: 12th August 2006, 21:41

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.