Results 1 to 9 of 9

Thread: Interpretting javascript using QScriptEngine

  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

  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: Interpretting javascript using QScriptEngine

    Could you print the contents of "str" in your program? If you are under Windows, maybe you should open the file in text mode as well? Does evaluating "1+2" return 3 properly?

  3. #3
    Join Date
    Jul 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Interpretting javascript using QScriptEngine

    I added tex mode flag to file open,
    bool bRes = scriptFile.open(QIODevice::ReadOnly|QIODevice::Tex t);
    still facing same problem.
    And please refer my answers to your questions inline.


    Could you print the contents of "str" in your program?
    >>>str = {"function hello()
    {
    var testStr = "Hello";
    return testStr;
    }" size=67}

    If you are under Windows, maybe you should open the file in text mode as well?
    >>> bool bRes = scriptFile.open(QIODevice::ReadOnly|QIODevice::Tex t);

    Does evaluating "1+2" return 3 properly?
    >>> returns correct value and works fine.
    ctor = engine.evaluate("2+1");
    bRes = ctor.isString();// returns false
    bRes = ctor.isNumber();// returns true
    bRes = ctor.isValid();// returns true
    bRes = ctor.isUndefined();// returns false
    bRes = ctor.isError();// returns false
    i = ctor.toInt32();// returns 3, correct value

    Thanks in advance...

    Thanks,
    Saravana.

  4. #4
    Join Date
    Jul 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Interpretting javascript using QScriptEngine

    Thanks a lot for your response.

    Also I've tried directly assigning the code to "str" as follows,

    QString str = QString("function hello(){var testStr=\"Hello\"; return testStr; }");

    Please suggest me any other way you know to make it work.


    Thanks,
    Saravana.

  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: Interpretting javascript using QScriptEngine

    What do isValid() and isObject() return?

  6. #6
    Join Date
    Jul 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Interpretting javascript using QScriptEngine

    I tried as per your suggestion, and IsObject() returns false for the pgm evaluation() but true for function evaluation.

    Please refer code below and my comment inline.

    Thanks in advance...

    Qt Code:
    1. QFile scriptFile("javascript.js");
    2.  
    3. bool bRes = scriptFile.open(QIODevice::ReadOnly|QIODevice::Text);
    4. str = scriptFile.readAll();
    5. bRes = engine.canEvaluate(str);
    6. QScriptValue ctor = engine.evaluate(str);
    7. bRes = ctor.isValid(); // returns true
    8. bRes = ctor.isObject(); // returns [B][U]false[/U][/B]
    9.  
    10. ctor = engine.evaluate("hello");
    11. bRes = ctor.isValid(); // returns true
    12. bRes = ctor.isObject(); // returns true
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. str = QString("function hello(){var testStr=\"Hello\"; return testStr; }");
    2. bRes = engine.canEvaluate(str);
    3. QScriptValue ctor = engine.evaluate(str);
    4. bRes = ctor.isValid(); // returns true
    5. bRes = ctor.isObject(); // returns [B][U]false[/U][/B]
    6.  
    7. ctor = engine.evaluate("hello");
    8. bRes = ctor.isValid(); // returns true
    9. bRes = ctor.isObject(); // returns true
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 29th July 2008 at 20:12. Reason: missing [code] tags

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

    Default Re: Interpretting javascript using QScriptEngine

    Please suggest a way to overcome this issue. Hope some/one of you would faced and solved the problem already.

    Thanks in advance.

  8. #8
    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: Interpretting javascript using QScriptEngine

    I'm writing a script interpreter based application right now and I admit I'm not having such problems. Type conversion works fine and evaluation of scripts read from file works as expected as well. Your problem might be that the function is defined in script, so the type can't be cast to QString because it was never a variant based variable. I don't know if this is the case (and I doubt it), but it could be. Are you using the newest Qt release?

  9. #9
    Join Date
    Jul 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Interpretting javascript using QScriptEngine

    Hi, Thanks for your reply.

    I use Qt 4.3.4...windows platform.

    I tried below script... still facing the same problem. could you please check the script below for any error?

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

    could you please send me a working sample(java script + qt code) to my email muruga.db@gmail.com?
    (windows version)

    Thanks,
    Saravanan.
    Last edited by jpn; 31st July 2008 at 21:47. Reason: missing [code] 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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.