Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QJSEngine, how to import other scripts?

QJSEngine, how to import other scripts?

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 3.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alhasni
    wrote on last edited by Alhasni
    #1

    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?

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Did you trying importing like the follows

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

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Alhasni
        wrote on last edited by Alhasni
        #3

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

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          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
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          A 1 Reply Last reply
          0
          • dheerendraD dheerendra

            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

            A Offline
            A Offline
            Alhasni
            wrote on last edited by
            #5

            @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
            }
            
            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

              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
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              A 1 Reply Last reply
              0
              • dheerendraD dheerendra

                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
                
                A Offline
                A Offline
                Alhasni
                wrote on last edited by
                #7

                @dheerendra

                I am not using QML

                1 Reply Last reply
                0
                • dheerendraD Offline
                  dheerendraD Offline
                  dheerendra
                  Qt Champions 2022
                  wrote on last edited by
                  #8

                  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
                  @Community Service
                  Certified Qt Specialist
                  http://www.pthinks.com

                  A 1 Reply Last reply
                  0
                  • dheerendraD dheerendra

                    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.

                    A Offline
                    A Offline
                    Alhasni
                    wrote on last edited by
                    #9

                    @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

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      Alhasni
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        dc343
                        wrote on last edited by
                        #11

                        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?

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved