Important: Please read the Qt Code of Conduct -

[SOLVED] import .js file in Qml without Resources

  • 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?

    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



    @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"

  • 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

  • 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@


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

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

  • 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.

  • the result is the same; the complete path is:


    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"

  • Moderators

    You can use this trick to temporarily disable the QRC context: "link":

  • [quote author="sierdzio" date="1421317439"]You can use this trick to temporarily disable the QRC context: "link":[/quote]

    It works!

    int main(int argc, char *argv[])
    QApplication app(argc, argv);

    QQmlApplicationEngine engine;
    QQmlContext *context = engine.rootContext();
    return app.exec();


  • Moderators

    Glad to hear it :-)

  • 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?

Log in to reply