Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QJSEngine, how to import other scripts?



  • When using QJSEngine's evaluate function, I get errors when I try to import another script as follows:

    import { sum } from "./math.mjs";
    
    sum(5, 6);
    

    The error is: Uncaught exception at line 1: SyntaxError: Unexpected token `import'

    I can workaround that error by wrapping the main script in a function, and importing it using QJSEngine's importModule:

    // This is saved as main.mjs
    import { sum } from "./math.mjs";
    
    export function main()
    {
    	 sum(5, 6);
    }
    

    Then I import main.mjs and run it:

    auto m = _jsEngine.importModule("main.mjs");
    QJSValue main = m.property("main");
    QJSValue result = main.call();
    

    Now I dont get the error and math.mjs is correctly imported in main.mjs. The problem is that importModule must take a file as input instead of a string (for example from a QPlainTextEdit), and it forces me to use an unnecessary function (main). Is there anyway to avoid that and get import to work with evaulate?


  • Qt Champions 2017

    Did you trying importing like the follows

    import "math.mjs" as AlhasniMethods
    AlhasniMethods.sum(5,6)
    


  • @dheerendra I still get the error Uncaught exception at line 1: SyntaxError: Unexpected token `import'


  • Qt Champions 2017

    Looks like you are trying to import one javascript inside other. If this is the case true like follows.

    .import "math.mjs" as AlhasniMethods

    Notice the "." before import statement



  • @dheerendra

    I get a different error now: Uncaught exception at line 4: ReferenceError: AlhasniMethods is not defined

    Evaluate:

    .import "math.js" as AlhasniMethods
    AlhasniMethods.sum(5,6)
    

    math.js:

    export function sum(left, right)
    {
        return left + right
    }
    

  • Qt Champions 2017

    Try something like this.

    /* Dheeru.js */
    function func1() {
        console.log("http://www.pthinks.com")
    }
    
    /*   MyMain.js */
    
    Qt.include("Dheeru.js")
    function func() {
        console.log("Method is called")
        func1();
    }
    
    // Inside the QML
    import "MyMain.js" as MyFuncs
    


  • @dheerendra

    I am not using QML


  • Qt Champions 2017

    Example also talks about how import one js inside another js. If you call method func() you should be able to call func1() method as well.



  • @dheerendra

    But I think those are QML specific.

    For example I get this error when I evaluate with QJSEngine:
    Uncaught exception at line 1: ReferenceError: Qt is not defined



  • According to this question, it doesn't appear that this is supported.



  • I've downloaded this lib and added this script as an "Add Existing files..." to qml.qrc. Then I tried to load the module in my own javascript file but fails, and here it is.
    According to https://doc.qt.io/qt-5/qtqml-javascript-imports.html#importing-a-javascript-resource-from-another-javascript-resource I get errors while trying to import another javascript file.
    I've also tried to use Qt.include() but it fails too.

    .import "math.js" as mathematics
    function f()
    {
        console.log(mathematics.sqrt(-4).toString())
    }
    ...
    QQmlApplicationEngine failed to load component
    Script qrc:/myscript.js unavailable
    Invalid import qualifier
    
    Qt.include("math.js");
    function f()
    {
        console.log(sqrt(-4).toString())
    }
    ...
    ReferenceError: sqrt is not defined
    

    So what should I do?


Log in to reply