Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] import .js file in Qml without Resources
QtWS25 Last Chance

[SOLVED] import .js file in Qml without Resources

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 4 Posters 5.0k 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.
  • T Offline
    T Offline
    Tycos
    wrote on last edited by
    #1

    Hello guys,

    I have this problem. I have a .js file in my filesystem (for example I have downloaded a new js in the folder where is the executable of my application) and I want execute it in my qml file without import it in Resources file.

    How I can do it? It's possible?

    main.qml
    @
    import QtQuick 2.4
    import QtQuick.Controls 1.3
    import QtQuick.Window 2.2
    import QtQuick.Dialogs 1.2

    Window {
    title: qsTr("Hello World")
    width: 640
    height: 480
    visible: true

    Loader {
        id: loadItem
        source: "test.qml"
    }
    
    Item {
        Component.onCompleted: loadItem.item
    }
    

    }

    @

    test.qml
    @import QtQuick 2.0
    import QtQuick.Controls 1.1
    import QtQuick.Controls.Styles 1.2

    import "test.js" as Test

    Rectangle {
    width: 100
    height: 100
    color: "red"

    Component.onCompleted: console.log(Test.testFunc())
    

    }
    @

    error: "qrc:/test.qml:5:1: Script qrc:/test.js unavailable"

    1 Reply Last reply
    0
    • GianlucaG Offline
      GianlucaG Offline
      Gianluca
      wrote on last edited by
      #2

      I suppose that because the qml file is into a Resource file, you have to specify the complete path of the test.js.
      In fact, from the error message, you can note that test.qml try to load test.js resolving the relative path to "qrc:/test.js"

      Try with full path.
      import "fullPath/test.js" as Test

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Tycos
        wrote on last edited by
        #3

        My dir structure is:

        workspace_QT - root dir
        - build-test-Desktop_Qt_5_4_0_GCC_64bit-Debug - build dir (where is 'test.js')
        - test - project dir

        I have modified it:

        @import "../build-test-Desktop_Qt_5_4_0_GCC_64bit-Debug/test.js" as Test@

        or

        @import "qrc:/test.js" as Test@

        but in both case it doesn't work :((( (file not found)

        1 Reply Last reply
        0
        • GianlucaG Offline
          GianlucaG Offline
          Gianluca
          wrote on last edited by
          #4

          No, you did wrong.
          the import is resolved at runtime, so you should put the path relative to the current working directory of the running app, not the relative path to the Qt project.
          So, just for test, first put a full complete path and then use a runtime variable based on the current working directory.

          @
          import "/full/path/from/root/test.js"
          @

          In my project, I have a variable based on the current working directory of the app to resolve the paths into QML:

          @
          import backend.commonPath()+"/test.js"
          @

          where backend is a C++ QObject with some utility methods exposed to QML side.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            Tycos
            wrote on last edited by
            #5

            the result is the same; the complete path is:

            "/home/tycos/workspace_QT/build-test-Desktop_Qt_5_4_0_GCC_64bit-Debug/test.js"

            I've modified it in this way:

            @import "/home/tycos/workspace_QT/build-test-Desktop_Qt_5_4_0_GCC_64bit-Debug/test.js" as Test@

            and the error in "application output" is:

            "qrc:/test.qml:5:1: Script qrc:/home/tycos/workspace_QT/build-test-Desktop_Qt_5_4_0_GCC_64bit-Debug/test.js unavailable
            qrc:/home/tycos/workspace_QT/build-test-Desktop_Qt_5_4_0_GCC_64bit-Debug/test.js: File not found"

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              You can use this trick to temporarily disable the QRC context: "link":https://github.com/sierdzio/closecombatfree/blob/master/src/ccfmain.cpp#L171.

              (Z(:^

              1 Reply Last reply
              0
              • T Offline
                T Offline
                Tycos
                wrote on last edited by
                #7

                [quote author="sierdzio" date="1421317439"]You can use this trick to temporarily disable the QRC context: "link":https://github.com/sierdzio/closecombatfree/blob/master/src/ccfmain.cpp#L171.[/quote]

                It works!

                main.qml
                @
                int main(int argc, char *argv[])
                {
                QApplication app(argc, argv);

                QQmlApplicationEngine engine;
                QQmlContext *context = engine.rootContext();
                 context->setBaseUrl(QUrl::fromLocalFile(""));
                 engine.load(QUrl(QStringLiteral("main.qml")));
                
                return app.exec();
                

                }
                @

                1 Reply Last reply
                0
                • sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #8

                  Glad to hear it :-)

                  (Z(:^

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    myQtQml
                    wrote on last edited by
                    #9

                    I have similar problem.
                    In my project, I cannot expose my main.qml file (must be binary & a part of exe). But the javascript file is user editable. Is this combination possible?

                    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