QJSEngine, error #2 (Problem after Qt update)
-
@SPlatten
Ok super.Sure but it lowers the number of people testing it as
one must make a project, alter the code a lot to make it compile
since its a member function.when we have to change the code a lot to make it run , there is a chance we introduce other errors
or simply not test the same.anyway, would you say this test it ?
void Test() { QJSEngine *mspobjJSeng = new class QJSEngine(); mspobjJSeng->installExtensions( QJSEngine::AllExtensions); //Added the tests below to illustrate it doesn't work, both error with 2 QJSValue err; err = mspobjJSeng->evaluate("(print(1 + 2);)"); if ( err.isError() ) { qDebug() << err.EvalError; } err = mspobjJSeng->evaluate("(console.log(1 + 2);"); if ( err.isError() ) { qDebug() << err.EvalError; } }
-
Sorry, I should have uploaded the example application I created when I posted the bug on the official site...
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QJSEngine test; test.installExtensions(QJSEngine::AllExtensions); QJSValue result = test.evaluate("(console.info(\"Hello World\");)"); qDebug() << result.EvalError; return 0; }
-
Hi
Tested some more.
i dont get the
" Indicates that console functions (console.log(), for example) should be installed."
error.so the error 2 comes from not being a value i guess.
auto text = R"( function f(a, b) { console.log("a is ", a, "b is ", b); } f(10,10); )"; QJSValue result = test.evaluate(text);
console seems to work.
js: a is 10 b is 10 2
so it does work for me on Win. No error about console.
-
My usage is similar I use console.info in a function and its not working, yes it was working in previous versions of Qt.
In my example even Hello World just a string doesn't work.
-
Sorry I really can't answer that, here is an update, just tried this:
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QJSEngine test; test.installExtensions(QJSEngine::AllExtensions); QJSValue result; result = test.evaluate("console.log(123);"); qDebug() << result.EvalError; result = test.evaluate("(console.log(456);)"); qDebug() << result.EvalError; result = test.evaluate("(console.log(\"Hello World\");)"); qDebug() << result.EvalError; return 0; }
The output from this was:
QML debugging is enabled. Only use this in a safe environment. 2020-04-10 13:15:53.563312+0100 scriptExample[1089:16542] QML Debugger: Waiting for connection on port 49396... 2020-04-10 13:15:54.515935+0100 scriptExample[1089:16432] [js] 123 2020-04-10 13:15:54.515978+0100 scriptExample[1089:16432] 2 2020-04-10 13:15:54.516023+0100 scriptExample[1089:16432] 2 2020-04-10 13:15:54.516132+0100 scriptExample[1089:16432] 2 2020-04-10 13:15:54.671516+0100 scriptExample[1089:16432] QObject::connect: No such signal QCoreApplication::focusObjectChanged(QObject*)
Interestingly, removing the brackets surrounding the console.log call and it worked. The addition / requirement of brackets around the JavaScript is a fairly new addition.
Another edit, I changed the last console.log to:
result = test.evaluate("function abc(t) { console.log(\"Hello World:\" + t);}abc(999);"); qDebug() << result.EvalError;
Output:
2020-04-10 13:23:59.005721+0100 scriptExample[1207:20216] [js] Hello World:999 2020-04-10 13:23:59.005735+0100 scriptExample[1207:20216] 2
Notice that result.EvalError is still 2 even though the script was executed perfectly and the output was as desired.
My conclusion is that the issue seems to be related to the introduction of wrapping content in brackets.
if I remove the brackets from the console.log that displays Hello World, that also works. -
There would seem to be multiple problems with QJSEngine, if I try to register a global then it must be wrapped in () or it fails:
"({\"db\":\"test\",\"host\":\"localhost\",\"usr\":\"root\",\"pass\":\"123456\"})"
If it is left as:
"{\"db\":\"test\",\"host\":\"localhost\",\"usr\":\"root\",\"pass\":\"123456\"}"
It fails with, EvalError: 2
It's not consistent, some things need to be wrapped in () or they fail others cannot be wrapped in () or they will fail, what is the reasoning for it and surely it would be better all round if the wrapping and handling of when or if to wrap in () was handled by the engine itself?
-
Trying alternate versions until I can find one that doesn't have this problem, so far I've tried:
5.14.2
5.13.2
5.12.8
5.12.2Then there is quite a jump to versions available on the MaintenanceTool to 5.9.9, which I'm downloading now.
Qt 5.9.9 works, I'm getting different syntax errors now, but at least the fundamental stuff is now working. Hope this gives the Qt developers something to go on.
Yep, 5.9.9 definitely works.