Can't import QML component using QRC



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



  • This post is deleted!

  • Moderators

    said in Can't import QML component using QRC:

    qrc:/components/navigation

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


  • Moderators

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



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



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


  • Moderators

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



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