Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Why does qml files fail to load if I separate the entry?
QtWS25 Last Chance

Why does qml files fail to load if I separate the entry?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
cmakeqtquickqrcrcc
1 Posts 1 Posters 540 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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 find main.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 the main.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>
    
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved