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. Can't import QML component using QRC
Forum Updated to NodeBB v4.3 + New Features

Can't import QML component using QRC

Scheduled Pinned Locked Moved QML and Qt Quick
qmlimportqqmlapplicationqrccomponents
8 Posts 5 Posters 12.5k Views 4 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.
  • J Offline
    J Offline
    Jason Wright
    wrote on 1 Dec 2016, 23:04 last edited by
    #1

    I have a QML project with the following directory structure:

    /
      project.pro
      main.cpp
      qml.qrc
      /qml
        /main
          main.qml
        /components
          Navigation.qml
    

    and my qml.qrc file is as follows:

    <RCC>
        <qresource prefix="/">
            <file alias="main">qml/main/main.qml</file>
        </qresource>
        <qresource prefix="/components">
            <file alias="navigation">qml/components/Navigation.qml</file>
        </qresource>
    </RCC>
    

    What I'm trying to do is use my Navigation component within main.qml. I cannot for the life of me figure out what the correct import statement should be. I've tried lots of variants of

    import "qrc:/components/navigation" as Navigation
    

    only to repeatedly get errors like

    QQmlApplicationEngine failed to load component 
    qrc:/main:5 "qrc:/components/navigation": no such directory
    

    I have tried messing with the file structure/naming/qrc prefixes/etc with basically no luck. Can anyone guide me on the proper way to do this?
    Thanks!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      cjmdaixi
      wrote on 2 Dec 2016, 09:12 last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • P Offline
        P Offline
        p3c0
        Moderators
        wrote on 2 Dec 2016, 09:26 last edited by
        #3

        said in Can't import QML component using QRC:

        qrc:/components/navigation

        Shouldn't it be qrc:/qml/components/navigation instead ?

        157

        R 1 Reply Last reply 2 Dec 2016, 09:48
        0
        • P p3c0
          2 Dec 2016, 09:26

          said in Can't import QML component using QRC:

          qrc:/components/navigation

          Shouldn't it be qrc:/qml/components/navigation instead ?

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 2 Dec 2016, 09:48 last edited by
          #4

          @p3c0 said in Can't import QML component using QRC:

          Shouldn't it be qrc:/qml/components/navigation instead ?

          no there is a "navigation" prefix specified (in the qrc file).

          @Jason-Wright
          What does QFileInfo(":/components/navigation").exists() return?
          Also make sure that the qrc file is rebuilt correctly. You can rerun qmake and to a rebuilt to make sure.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          J 1 Reply Last reply 2 Dec 2016, 22:58
          2
          • R raven-worx
            2 Dec 2016, 09:48

            @p3c0 said in Can't import QML component using QRC:

            Shouldn't it be qrc:/qml/components/navigation instead ?

            no there is a "navigation" prefix specified (in the qrc file).

            @Jason-Wright
            What does QFileInfo(":/components/navigation").exists() return?
            Also make sure that the qrc file is rebuilt correctly. You can rerun qmake and to a rebuilt to make sure.

            J Offline
            J Offline
            Jason Wright
            wrote on 2 Dec 2016, 22:58 last edited by
            #5

            @raven-worx thanks for your response!

            QFileInfo(":/components/navigation").exists() returns true.
            I've also tried clean builds & re-running qmake, but no luck so far :-(

            J 1 Reply Last reply 2 Dec 2016, 23:10
            0
            • J Jason Wright
              2 Dec 2016, 22:58

              @raven-worx thanks for your response!

              QFileInfo(":/components/navigation").exists() returns true.
              I've also tried clean builds & re-running qmake, but no luck so far :-(

              J Offline
              J Offline
              Jason Wright
              wrote on 2 Dec 2016, 23:10 last edited by Jason Wright 12 Feb 2016, 23:14
              #6

              @Jason-Wright

              I did eventually get this to work by doing the following:

              changed the qrc file to

              <RCC>
                  <qresource prefix="/">
                      <file alias="main">qml/main/main.qml</file>
                  </qresource>
                  <qresource prefix="/components">
                      <file alias="Navigation.qml">qml/components/Navigation.qml</file>
                  </qresource>
              </RCC>
              

              changed the import statement in main.qml to

              import "./components" as Components
              

              and subsequently using

              Components.Navigation { id: navigation }
              

              which then causes a lookup of Navigation.qml (and yeah, I did still have to give it an alias in the .qrc file, for some reason...)

              Not sure why I had to do this from my reading of the documentation, but it's at least a workable solution.

              R H 2 Replies Last reply 3 Dec 2016, 12:17
              1
              • J Jason Wright
                2 Dec 2016, 23:10

                @Jason-Wright

                I did eventually get this to work by doing the following:

                changed the qrc file to

                <RCC>
                    <qresource prefix="/">
                        <file alias="main">qml/main/main.qml</file>
                    </qresource>
                    <qresource prefix="/components">
                        <file alias="Navigation.qml">qml/components/Navigation.qml</file>
                    </qresource>
                </RCC>
                

                changed the import statement in main.qml to

                import "./components" as Components
                

                and subsequently using

                Components.Navigation { id: navigation }
                

                which then causes a lookup of Navigation.qml (and yeah, I did still have to give it an alias in the .qrc file, for some reason...)

                Not sure why I had to do this from my reading of the documentation, but it's at least a workable solution.

                R Offline
                R Offline
                raven-worx
                Moderators
                wrote on 3 Dec 2016, 12:17 last edited by
                #7

                @Jason-Wright said in Can't import QML component using QRC:

                which then causes a lookup of Navigation.qml (and yeah, I did still have to give it an alias in the .qrc file, for some reason...)
                Not sure why I had to do this from my reading of the documentation, but it's at least a workable solution.

                Seems like QML only resolves components only with .qml extension.

                Your solution wokrs because import "./components" as Components imports the whole folder 'navigation'. Then you use the component Components.Navigation, which then is loaded by the engine as Navigation.qml.

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0
                • J Jason Wright
                  2 Dec 2016, 23:10

                  @Jason-Wright

                  I did eventually get this to work by doing the following:

                  changed the qrc file to

                  <RCC>
                      <qresource prefix="/">
                          <file alias="main">qml/main/main.qml</file>
                      </qresource>
                      <qresource prefix="/components">
                          <file alias="Navigation.qml">qml/components/Navigation.qml</file>
                      </qresource>
                  </RCC>
                  

                  changed the import statement in main.qml to

                  import "./components" as Components
                  

                  and subsequently using

                  Components.Navigation { id: navigation }
                  

                  which then causes a lookup of Navigation.qml (and yeah, I did still have to give it an alias in the .qrc file, for some reason...)

                  Not sure why I had to do this from my reading of the documentation, but it's at least a workable solution.

                  H Offline
                  H Offline
                  Hit Tiger Tonight
                  wrote on 17 Jan 2023, 03:16 last edited by
                  #8

                  @Jason-Wright Thanks for your solution. Help me solve the same problem.
                  You can also remove 'as Components', then don't care this alias when use your 'Navigation'.

                  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