Results 1 to 9 of 9

Thread: Interpretting javascript using QScriptEngine

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Interpretting javascript using QScriptEngine

    Hi there,

    I'm trying to use use QScriptEngine to call a java script method.

    Please refer below code, And I mentioned return values of each statement in comments.
    Please correct me if my usage is wrong... My requirement is simple, I need to execute a method(which returns a string) present in javascript file and need to use returned string in my Qt program.

    Thanks in advance...

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4.  
    5. QScriptEngine engine;
    6. QScriptValue global = engine.globalObject();
    7. QString str, str1, str2; int i=0;
    8. QFile scriptFile("javascript.js");
    9. // QFile scriptFile("New.html");
    10.  
    11. bool bRes = scriptFile.open(QIODevice::ReadOnly); // returns true
    12. str = scriptFile.readAll(); // returns correct file content /script
    13. bRes = engine.canEvaluate(str); // returns true
    14. QScriptValue ctor = engine.evaluate(str);
    15. scriptFile.close();
    16. bRes = ctor.isError(); // returns false
    17. str1 = ctor.toString(); // returns "undefined"
    18. bRes = ctor.isString();// returns false
    19.  
    20. ctor = engine.evaluate("Hello");
    21. bRes = ctor.isString(); // returns false
    22. bRes = ctor.isValid();// returns true
    23. bRes = ctor.isUndefined();// returns false
    24. bRes = ctor.isError();// returns true
    25. str2 = ctor.toString();// returns "ReferenceError: Hello is not defined"
    26. i = ctor.toInt32(); // returns 0
    27.  
    28. return app.exec();
    29.  
    30. }
    To copy to clipboard, switch view to plain text mode 

    javascript.js:
    javascript Code:
    1. function hello()
    2. {
    3. var testStr = "Hello";
    4. return testStr;
    5. }
    To copy to clipboard, switch view to plain text mode 

    Regards,
    Saravana
    Last edited by wysota; 26th July 2008 at 08:51. Reason: Missing tags

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.