"No such file or directory" error qrc
-
wrote on 5 Apr 2025, 09:28 last edited by
Hey all,
I've just been playing around with QT for the first time, but I've hit a roadblock with the RCC system.
To my knowledge, the system compiles resources alongside the executable and stores them as one file, which sounds great, but I continue to get the error:QQmlApplicationEngine failed to load component qrc:/main.qml: No such file or directory
Whenever I attempt to run my project. I have four files in my root directory, CMakeLists.txt, main.cpp, resources.qrc and a folder named 'ui' that contains main.qml.
From what I can see from the online resources, everything should be working, and my code complies with what is said in the documentation. I am developing via jetbrains CLion on fedora linux with QT 6.9, but I doubt the issue is caused by problems with CLion as everything else has functioned flawlessly.What the program should do is launch a simple window with the text "Welcome", by loading the main.qml file as a little test platform.
Any help would be greatly appreciated.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.30) project(test) set(CMAKE_CXX_STANDARD 20) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_AUTOUIC ON) set(CMAKE_PREFIX_PATH "/home/user/Qt/6.9.0/gcc_64/") find_package(Qt6 COMPONENTS Core Qml Gui Widgets Quick REQUIRED) #set(CMAKE_AUTORCC_SEARCH_PATHS ${CMAKE_SOURCE_DIR}) add_executable(test main.cpp) qt_add_resources(test resources.qrc) target_link_libraries(test Qt::Core Qt::Gui Qt::Widgets Qt::Quick Qt::Qml )
main.cpp:
#include <iostream> #include <QApplication> #include <QGuiApplication> #include <QQmlApplicationEngine> int main(int argc, char *argv[]) { { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; /*QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed, &app, []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection);*/ engine.load("qrc://ui/main.qml"); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); } }
resources.qrc:
#include <iostream> #include <QApplication> #include <QGuiApplication> #include <QQmlApplicationEngine> int main(int argc, char *argv[]) { { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; /*QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed, &app, []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection);*/ engine.load("qrc://ui/main.qml"); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); } }
main.qml:
import QtQuick 2.15 import QtQuick.Window 2.15 Window { id: mainWindow width: 640 height: 480 visible: true title: qsTr("test") Text { anchors.centerIn: parent text: qsTr("Welcome.") } }```
-
You qresources.qrc is wrongly pasted I would guess.
engine.load("qrc://ui/main.qml");
I would be surprised if Qt would complain about
qrc:/main.qml
then. -
You qresources.qrc is wrongly pasted I would guess.
engine.load("qrc://ui/main.qml");
I would be surprised if Qt would complain about
qrc:/main.qml
then.wrote on 5 Apr 2025, 12:18 last edited by@Christian-Ehrlicher
Unfortunately it seems like everything it correctly pasted. All URIs for the main.qml file are identical, and modifying the project so that main.qml is in the root directory and all URIs point to that new location still returns the same error. -
Lifetime Qt Championwrote on 5 Apr 2025, 12:53 last edited by Christian Ehrlicher 5 days ago
What you posted for resources.qrc if no xml but c++ code...
-
What you posted for resources.qrc if no xml but c++ code...
wrote on 5 Apr 2025, 13:18 last edited by AstroFinch 5 days ago@Christian-Ehrlicher Oh yes, sorry, I must have accidentally pasted the CPP twice instead of copying the .qrc.
EDIT: I can't seem to edit the original post anymore, so I've appended the .qrc here:
<!DOCTYPE RCC> <RCC> <qresource> <file>main.qml</file> </qresource> </RCC>
-
Lifetime Qt Championwrote on 5 Apr 2025, 18:16 last edited by Christian Ehrlicher 5 days ago
It must be
QUrl("qrc:/main.qml")
or":/main.qml"
according to https://doc.qt.io/qt-6/resources.html#qt-resource-collection-file-qrc and https://doc.qt.io/qt-6/resources.html#runtime-api -
wrote on 7 Apr 2025, 12:03 last edited by
SOLVED:
I eventually discovered this little section in the QT docs: https://doc.qt.io/qt-6/resources.html#cmake
Turns out that resources.qrc that linked to main.qml needed to be listed as an executable in CMakeLists.txt, in the line:
add_executable(test main.cpp resources.qrc)
I would have assumed that a qrc file would logically be listed as a resources using:
qt_add_resources(test resources.qrc)
but apparently not. main.qml was nowhere to be seen in the QRC file system, but after adding the .qrc file as an executable it has appeared and it usable.
-
-
SOLVED:
I eventually discovered this little section in the QT docs: https://doc.qt.io/qt-6/resources.html#cmake
Turns out that resources.qrc that linked to main.qml needed to be listed as an executable in CMakeLists.txt, in the line:
add_executable(test main.cpp resources.qrc)
I would have assumed that a qrc file would logically be listed as a resources using:
qt_add_resources(test resources.qrc)
but apparently not. main.qml was nowhere to be seen in the QRC file system, but after adding the .qrc file as an executable it has appeared and it usable.
@AstroFinch said in "No such file or directory" error qrc:
needed to be listed as an executable
It is not listed as an executable (your executable is named "test"). It is listed as a source item to build your executable (just like main.cpp).
1/8