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] How to import a QML component from resource file in a Qt Quick application using resource file based QML files?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 16.7k Views 1 Watching
  • 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.
  • K Offline
    K Offline
    kuschky
    wrote on last edited by
    #1

    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

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

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

      (Z(:^

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kuschky
        wrote on last edited by
        #3

        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

        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