Why does qml files fail to load if I separate the entry?
Unsolved
QML and Qt Quick
-
I have the following directory:
. ├── CMakeLists.txt └── src ├── CMakeLists.txt ├── main.cpp └── radon ├── main.qml ├── qml.qrc ├── radon.cpp └── radon.h
./CMakeLists.txt
:cmake_minimum_required(VERSION 3.15) project(Radon LANGUAGES CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) add_subdirectory(src)
src/CMakeLists.txt
is:set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) find_package(Qt5 5.15 REQUIRED COMPONENTS Core Quick REQUIRED) add_library(RadonCore radon/radon.h radon/radon.cpp radon/qml.qrc ) target_compile_definitions(RadonCore PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>) target_link_libraries(RadonCore PUBLIC Qt5::Core Qt5::Quick) add_executable(RadonApp main.cpp) target_link_libraries(RadonApp PRIVATE RadonCore)
Right now, the
radon.h
simply contains the entry to my application, its definition is as follows:
radon.h
:#ifndef RADON_RADON_H #define RADON_RADON_H namespace radon{ int radon_entry(int argc, char* argv[]); } #endif //RADON_RADON_H
radon.cpp
:#include "radon.h" #include <QGuiApplication> #include <QQmlApplicationEngine> int radon::radon_entry(int argc, char* argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); return app.exec(); }
The
main.cpp
just calls the entry point:#include "radon/radon.h" int main(int argc, char* argv[]) { return radon::radon_entry(argc, argv); }
The idea is to provide my own
main
in tests. However, Qt complains about not able to findmain.qml
:QQmlApplicationEngine failed to load component qrc:/main.qml: No such file or directory
If I provide a main in
radon.cpp
and build it as an executable then it is able to find themain.qml
. Why is this so? How can I separate the entry to my app?qml.qrc
is as follows:<RCC> <qresource prefix="/"> <file>main.qml</file> </qresource> </RCC>