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...
int main(int argc, char *argv[])
{
QScriptEngine engine;
QScriptValue global = engine.globalObject();
QFile scriptFile
("javascript.js");
// QFile scriptFile("New.html");
bool bRes
= scriptFile.
open(QIODevice::ReadOnly);
// returns true str = scriptFile.readAll(); // returns correct file content /script
bRes = engine.canEvaluate(str); // returns true
QScriptValue ctor = engine.evaluate(str);
scriptFile.close();
bRes = ctor.isError(); // returns false
str1 = ctor.toString(); // returns "undefined"
bRes = ctor.isString();// returns false
ctor = engine.evaluate("Hello");
bRes = ctor.isString(); // returns false
bRes = ctor.isValid();// returns true
bRes = ctor.isUndefined();// returns false
bRes = ctor.isError();// returns true
str2 = ctor.toString();// returns "ReferenceError: Hello is not defined"
i = ctor.toInt32(); // returns 0
return app.exec();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QScriptEngine engine;
QScriptValue global = engine.globalObject();
QString str, str1, str2; int i=0;
QFile scriptFile("javascript.js");
// QFile scriptFile("New.html");
bool bRes = scriptFile.open(QIODevice::ReadOnly); // returns true
str = scriptFile.readAll(); // returns correct file content /script
bRes = engine.canEvaluate(str); // returns true
QScriptValue ctor = engine.evaluate(str);
scriptFile.close();
bRes = ctor.isError(); // returns false
str1 = ctor.toString(); // returns "undefined"
bRes = ctor.isString();// returns false
ctor = engine.evaluate("Hello");
bRes = ctor.isString(); // returns false
bRes = ctor.isValid();// returns true
bRes = ctor.isUndefined();// returns false
bRes = ctor.isError();// returns true
str2 = ctor.toString();// returns "ReferenceError: Hello is not defined"
i = ctor.toInt32(); // returns 0
return app.exec();
}
To copy to clipboard, switch view to plain text mode
javascript.js:
function hello()
{
var testStr = "Hello";
return testStr;
}
function hello()
{
var testStr = "Hello";
return testStr;
}
To copy to clipboard, switch view to plain text mode
Regards,
Saravana
Bookmarks