Results 1 to 3 of 3

Thread: qt script property

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

    Default qt script property

    I want set a property like xxx.xxx.xxx in qt scirpt ,so I write code like these:

    Qt Code:
    1. QScriptEngine engine;
    2. QScriptValue o = engine.globalObject();
    3. QScriptValue oo;
    4. QScriptValue o1;
    5.  
    6. o1 = o.property("org");
    7. if (!o1.isValid()) {
    8. o1 = engine.newObject();
    9. o.setProperty("org", o1);
    10. }
    11.  
    12. o1= o.property("org.math");
    13. if (!o1.isValid()) {
    14. o1 = engine.newObject();
    15. o.setProperty("math", o1);
    16. }
    17.  
    18. engine.evaluate("org.sum = function Math_sum(a){ return 123456; }");
    19. engine.evaluate("org.math.sum = function Math_sum(a){ return 654321; }");
    20. engine.evaluate("var r = org.sum(12); print(r); ");
    21. engine.evaluate("var r1 = org.math.sum(12); print(r1); ");
    To copy to clipboard, switch view to plain text mode 

    output is: 123456
    so the org.math.sum is invalid.

    I have try this way too:
    Qt Code:
    1. QScriptValue o = engine.globalObject();
    2. QScriptValue oo;
    3. QScriptValue o1;
    4.  
    5. o1 = o.property("org");
    6. if (!o1.isValid()) {
    7. o1 = engine.newObject();
    8. o.setProperty("org", o1);
    9. }
    10.  
    11. QScriptValue o2;
    12. o2= o1.property("math");
    13. if (!o2.isValid()) {
    14. o2 = engine.newObject();
    15. o.setProperty("math", o2);
    16. }
    To copy to clipboard, switch view to plain text mode 

    but the output still is 12345.

    If i want excute function org.math.sum(), how should I set the property.

  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: qt script property

    I think line #12 of your first snippet is wrong. You can transit through properties like that, you have to write it like so:
    Qt Code:
    1. o1= o.property("org").property("math");
    To copy to clipboard, switch view to plain text mode 

    Furthermore line #15 sets a property "math" of the global object and not of the "org" object so org.math will be undefined.
    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. #3
    Join Date
    Jun 2010
    Posts
    38
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qt script property

    Thanks..

    follow your suggestion ,I change my codes ,and it does work.

    Qt Code:
    1. QScriptEngine engine;
    2. QScriptValue o = engine.globalObject();
    3. QScriptValue o1;
    4. QScriptValue o2;
    5.  
    6. o1 = o.property("org");
    7. if (!o1.isValid()) {
    8. o1 = engine.newObject();
    9. o.setProperty("org", o1);
    10. }
    11.  
    12.  
    13. o2= o.property("org").property("math");
    14.  
    15. if (!o2.isValid()) {
    16. o2 = engine.newObject();
    17.  
    18. o1.setProperty("math", o2);
    19. }
    20.  
    21. engine.evaluate("org.sum = function Math_sum(){ return 123456; }");
    22. engine.evaluate("org.math.sum = function Math_sum(){ return 654321; }");
    23. engine.evaluate("var r = org.sum(); print(r); ");
    24. engine.evaluate("var r1 = org.math.sum(); print(r1); ");
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. script
    By wookoon in forum Newbie
    Replies: 1
    Last Post: 19th July 2010, 09:47
  2. Using QT Script...
    By minhaz in forum Newbie
    Replies: 1
    Last Post: 18th August 2009, 22:29
  3. Qt Script
    By c_srikanth1984 in forum Qt Programming
    Replies: 3
    Last Post: 9th June 2009, 10:35
  4. Can I include a script from script?
    By yycking in forum Qt Programming
    Replies: 1
    Last Post: 24th April 2009, 03:01
  5. Qt script
    By rajesh_clt3 in forum Qt Programming
    Replies: 2
    Last Post: 27th July 2007, 12:40

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.