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
QtWS25 Last Chance

Can't import QML component using QRC

Scheduled Pinned Locked Moved QML and Qt Quick
qmlimportqqmlapplicationqrccomponents
8 Posts 5 Posters 12.4k 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.
  • J Offline
    J Offline
    Jason Wright
    wrote on 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
    • cjmdaixiC Offline
      cjmdaixiC Offline
      cjmdaixi
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on 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

        raven-worxR 1 Reply Last reply
        0
        • p3c0P p3c0

          said in Can't import QML component using QRC:

          qrc:/components/navigation

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

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on 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
          • raven-worxR raven-worx

            @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 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
            0
            • J Jason Wright

              @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 last edited by Jason Wright
              #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.

              raven-worxR H 2 Replies Last reply
              1
              • J Jason Wright

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

                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on 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

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