Calling a function inside of a *.qs file
-
wrote on 6 Aug 2024, 19:58 last edited by
Hi,
I wanted to customize dynamically my small app (Qt 6.5.0, windows & linux), and therefore make use of script module.
I want to call a javascript function from an external *.qs file.
In my C++ app, I have tried this to call the script like this:void MainWindow::onRunScript() { QJSEngine myEngine; QString fileName = "run.qs"; QFile scriptFile(fileName); if (!scriptFile.open(QIODevice::ReadOnly)) { // handle error qDebug() << "MainWindow::onRunScript : unable to open " << fileName; } QTextStream stream(&scriptFile); QString contents = stream.readAll(); scriptFile.close(); QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership); QJSValue LogicValue = myEngine.newQObject(this); myEngine.globalObject().setProperty("Main", LogicValue); QJSValue val = myEngine.evaluate(contents); if(val.isError()) { qDebug() << val.errorType(); } }
No error until this, my file run.qs exists, and QJSValue resulted from QJSEngine is no error.
Here is my run.qs file:(function() { Main.write(57,64); })
Unfortunately, MainWindow::write(int,int) is never called.
I guess, the issue is in the *.qs file. I have added the outer parenthesis because of a warning from compiler
I didn't find any example to make this work. I barely remember of a *.mjs file that enabled several functions stand in the same file, but I did not find it either.
Can anyone give me a link to an explanation about the external javascript files format please?
thank you,
Claude
1/1