QML file not found in resource system (qrc) despite correct qml.qrc configuration
-
Hello,
I'm facing a persistent issue with a Qt Quick application using Qt 6.9.1 (also reproduced in Qt 5.15.8):Despite having correctly defined my qml.qrc and file structure, QQmlApplicationEngine fails to load my QML file, reporting:
QQmlApplicationEngine failed to load component qrc:/ui/qml/Screen.qml: No such file or directory QML file exists? false Files in qrc:/ui/qml: QList("ui")
My setup:
Qt version: 6.9.1 (MinGW 64-bit) and 5.15.8 (tested on both)OS: Windows 10
Using CMake
Project structure:
/project-root ├── src/ │ └── main.cpp ├── ui/qml/ │ └── Screen.qml ├── qml.qrc ├── CMakeLists.txt
qml.qrc content:
<RCC> <qresource prefix="/ui/qml"> <file>ui/qml/Screen.qml</file> </qresource> </RCC>
CMakeLists.txt (relevant part):
qt_add_executable(MotorsportApp src/main.cpp qml.qrc ) target_link_libraries(MotorsportApp PRIVATE Qt6::Core Qt6::Gui Qt6::Qml Qt6::Quick)
main.cpp (minimal test):
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QDebug> #include <QDir> #include <QFile> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; qDebug() << "Files in qrc:/ui/qml:" << QDir(":/ui/qml").entryList(); qDebug() << "QML file exists?" << QFile::exists(":/ui/qml/Screen.qml"); engine.load(QUrl(QStringLiteral("qrc:/ui/qml/Screen.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); }
What I've tried:
Clean build, delete build folderReimport project in Qt Creator
Encoding of qml.qrc set to UTF-8 without BOM
Verified that Screen.qml exists at the expected path
Resource path is referenced exactly as in .qrc
Verified qml.qrc is included in CMake
Also tried qt_add_resources() with PREFIX and BASE
Still, at runtime, the resource system only shows "ui" in qrc:/ui/qml/, and not the actual file.
Any idea what could be causing this? Is this a bug in qmlimportscanner or rcc on Windows?
Thanks in advance!
-
So your path is ui/qml/ui /qml which is not what you pass to engine.load(). Fix your CMakeLists.txt or your call to load()
-
@Actarus
It sounds like you got messed up at some point and created unintended repeats of folders?<qresource prefix="/ui/qml"> <file>ui/qml/Screen.qml</file> </qresource>
Feels like one of
prefix=
or<file>
path is duplicate. This is going to deliver/ui/qml/ui/qml/Screen.qml
. -
So your path is ui/qml/ui /qml which is not what you pass to engine.load(). Fix your CMakeLists.txt or your call to load()
@Christian-Ehrlicher Thanks Christian, solved using qt_add_resources() instead.
-
@Actarus
It sounds like you got messed up at some point and created unintended repeats of folders?<qresource prefix="/ui/qml"> <file>ui/qml/Screen.qml</file> </qresource>
Feels like one of
prefix=
or<file>
path is duplicate. This is going to deliver/ui/qml/ui/qml/Screen.qml
. -
-
@JonB Thanks Jon, that was the reason but even without duplication it didn't work, so I solved it using qt_add_resources() instead.
@Actarus said in QML file not found in resource system (qrc) despite correct qml.qrc configuration:
I solved it using qt_add_resources() instead
I'm glad to hear that you were able to resolve your issue.
As the next step, I recommend taking the time to port from
qt_add_resources()
toqt_add_qml_module()
: https://doc.qt.io/qt-6/qt6-port-to-qt-add-qml-module.htmlThis provides your projects with benefits such as optimization by the Qt Quick Compiler (https://www.qt.io/blog/the-numbers-performance-benefits-of-the-new-qt-quick-compiler) and improved code analysis by your IDE.