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! -
said in Can't import QML component using QRC:
qrc:/components/navigation
Shouldn't it be
qrc:/qml/components/navigation
instead ? -
@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 doesQFileInfo(":/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()
returnstrue
.
I've also tried clean builds & re-running qmake, but no luck so far :-( -
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.
-
@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 componentComponents.Navigation
, which then is loaded by the engine asNavigation.qml
. -
@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'.