[Solved] How to import a QML component from resource file in a Qt Quick application using resource file based QML files?



  • Hello, I wrote a Qt Quick QML application loading all QML resources from a resource file. All files are on the same directory level and everything works well.

    Now I want to add a QML Component with sub - components placed in a subdirectory.

    The path structure inside the resource file is

    /qml/myapp/
    /qml/myapp/mycomponent

    On the path /qml/myapp/ is my main QML file located (which works without import)

    and in /qml/myapp/mycomponent is the MyComponent.qml which I want to import to use it.

    On a file systen based QML application I just do a import "myComponent" in the head of my QML File but in the resource file based version it does not work for me.

    @import QtQuick 1.0
    import "mycomponent"

    Rectangle {
    id: testScreen
    width: 1024
    height: 768
    color: "#ffffffff"

    MyComponent {

    }
    }@

    Can anybody tellme what the correct syntax? Or is it not possible to import another QML component from a resource file if the root QML file is already located in the resource file?

    Thanks Michael


  • Moderators

    @
    import ":/mycomponent"
    // or
    import "qrc:/mycomponent"
    // or:
    import "qrc:/qml/myapp/mycomponent"
    @

    Sorry for so many possible answers, I don't remember which one worked for me.



  • Hello, thanks for the reply but first it did not work like described.
    Nevertheless the problem is solved now. After deleting the Makefiles and recreate them by rebuilding the application it works now on the following way.

    Resources on the hardisk are in the projects subfolder

    qml/QmlTest1/main.qml
    qml/QmlTest1/mycomponent/MyComponent.qml

    Ressources inside the resource file all are with prefix "/" and no alias

    /qml/QmlTest1/main.qml
    /qml/QmlTest1/mycomponent/MyComponent.qml

    @import QtQuick 1.1

    import "mycomponent"

    Rectangle {
    width: 360
    height: 360
    Text {
    text: qsTr("Hello World")
    anchors.centerIn: parent
    }
    MouseArea {
    anchors.fill: parent
    onClicked: {
    Qt.quit();
    }
    }

    MyComponent  {
        x: 0;
        y: 0;
    }
    

    }@

    Also the syntax

    @import "qrc:/qml/QmlTest1/mycomponent"@

    proposed by sierdzio does work for absolute paths inside ressource files.

    For relative paths inside resource files

    @import "qrc:mycomponent"@

    does work.

    Everything runs with Qt 4.8.4 and QtCreator 2.5.2 on Windows7 64bit

    Thanks


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.