calling javascript function from Qt Console application
-
I have a js function that i need to evaluate from qt console application, and i am trying to learnt the QScriptEngine
so far i could read my function
double a; a=engine.evaluate("(myfunc(2,3))").toNumber();
in my simplejsfn.js i have
function myfunc(p1, p2) { return p1*p2; }
but i dont get the expected output
-
but i dont get the expected output
It always helps then if you say what you do get as the output!! (E.g. for all I know, you haven't included your
simplejsfn.js
, so the function isn't defined.)Although I know nothing about this, why not start out from a simpler, non-function expression, e.g. as per the docs example
myEngine.evaluate("1 + 2");
. Does that work?Then the docs go on to give a simple function example:
QScriptValue fun = myEngine.evaluate("(function(a, b) { return a + b; })"); QScriptValueList args; args << 1 << 2; QScriptValue threeAgain = fun.call(QScriptValue(), args);
Does that work?
That's how I'd approach it....
-
@JonB said in calling javascript function from Qt Consolt application:
QScriptValue threeAgain = fun.call(QScriptValue(), args);
I read the js function from a js file
QScriptEngine engine; QString fileName(":/simplejsfn.js"); QFile scriptFile(fileName); scriptFile.open(QIODevice::ReadOnly); QTextStream stream(&scriptFile); QString contents = stream.readAll(); scriptFile.close(); QScriptValue fun = engine.evaluate(contents); QScriptValueList args; args << 1 << 2; QScriptValue threeAgain = fun.call(QScriptValue(), args);
The js file has the following
function myfunc(p1, p2) { return p1*p2; }
while debugging i see the qstring in content with the following
"function myfunc(p1, p2) { return p1*p2; }\n"
The value of threeAgain is invalid ..
When i write the function as you do inside the evaluate it works, however when the function is inside a QString it does not work !
-
-
@SherifOmran Did you try to call https://doc.qt.io/qt-5/qscriptengine.html#uncaughtException to see whether there is an exception and if so what it is?
-
i did not try because this is a very simple example. I took another approach to create a browser and load the js file
-
@SherifOmran
Creating a browser, if you do not want it for other purposes, just to evaluate some JS is a very "heavyweight" solution. Just saying.... -
@SherifOmran You're adding a browser because it is a "very simple example"?!
In my opinion it would be way faster and easier to check for exceptions instead (you should do this anyway)... -
yes that is true, and it will be heavy, but i intent to run very complicated js file and if i can not run a simple test function, how will it be with a complicated script !
If any body can provide me a working example, this would be great. -
qDebug() << contents; QScriptValue fun = engine.evaluate(contents); qDebug() << engine.hasUncaughtException(); QScriptValueList args; args << 1 << 2; QScriptValue threeAgain = fun.call(QScriptValue(), args); qDebug() << engine.hasUncaughtException(); qDebug() << threeAgain.isValid();
would be an improvement which would take you 10 seconds.
-
@JonB said in calling javascript function from Qt Console application:
qDebug() << engine.hasUncaughtException();
Same result ! I am compiling under Mac high sierra 10.13.6
QString contents = "function myfunc(p1, p2) { return p1*p2; }"; QScriptValue fun = engine.evaluate(contents); qDebug() << engine.hasUncaughtException(); QScriptValueList args; args << 1 << 2; QScriptValue threeAgain = fun.call(QScriptValue(), args); qWarning() << engine.hasUncaughtException(); bool A= threeAgain.isValid();
-
Please test it on your side and let me know, if this simple code works. May be this is a bug ? I am using clang under mac high sierra.