Is it possible to import javascript files in a QJSEngine (QT 5.6)?
Example 1:
Text in plainTextEdit:
.import "./jsimport.js" as Test
var x = Test.y;
console.log(x);
c++:
void MainWindow::on_pushButton_clicked()
{
QJSEngine engine;
engine.installExtensions(QJSEngine::AllExtensions);
QJSValue result = engine.evaluate(ui->plainTextEdit->toPlainText());
qDebug() << "isError:" << result.isError();
qDebug() << "resultString:" << result.toString();
if(result.hasProperty("lineNumber"))
qDebug() << "property lineNumber:" << result.property("lineNumber").toInt();
}
text in jsimport.js file:
var y = 42;
result:
isError: true
resultString: "ReferenceError: Test is not defined"
property lineNumber: 2
Example 2:
Text in PlainTextEdit:
.import "./jsimport.js"
same c++ code
result:
isError: true
resultString: "SyntaxError: File import requires a qualifier"
property lineNumber: 1
It seems that there is some functionality for an import, because of the last error.
The .import statement does not work with QJSEngine. QJSEngine is just a bare interpreter, if you want to have some "import" functionality you might switch to QQmlEngine, which is built on top of QJSEngine: http://doc.qt.io/qt-5/qtqml-syntax-imports.html#qml-import-path
With QJSEngine you basically need to manually populate the js global object (and QtCreator sintax checker will not recognize the statements you use across the different files).