Custom C++ type from a QML module is not defined when used inside QML file

In my project I’m trying to expose a singleton type to QML from C++. Initially I registered it with qmlRegisterSingletonInstance and it worked fine. But when I tried to replace this way with QML_ELEMENT and QML_SINGLETON macros it compiles fine but when QML tries to use the class it complains that the type is not defined.

My project has two modules, one consists solely of qml files and the second – of C++ files, where I try to expose the class from and use it in the first module. The structure is a bit complicated so I reproduced the issue with a simpler test project. I noticed that QML cannot recognize neither the singleton type nor a non-singleton type.

The project has a following structure:

│   CMakeLists.txt
│   main.cpp
│
└───logicmodule
        CMakeLists.txt
        Main.qml
        Message.h
        MySingleton.h

The code:

// logicmodule/MySingleton.h
#pragma once

#include <QObject>
#include <QQmlEngine>
#include <QString>

class MySingleton : public QObject
{
    Q_OBJECT
    QML_ELEMENT
    QML_SINGLETON
public:
    MySingleton(QObject *parent = nullptr)
        : QObject{parent}
    {}

public slots:
    QString exec(const QString &str) { return "somestring"; }
};
// logicmodule/Message.h
#pragma once

#include <QObject>
#include <QQmlEngine>
#include <QString>

class Message : public QObject
{
    Q_OBJECT
    QML_ELEMENT
    Q_PROPERTY(QString author MEMBER m_author NOTIFY authorChanged)
public:
    Message() {}

signals:
    void authorChanged();

private:
    QString m_author;
};
// logicmodule/Main.qml
import QtQuick
import QtQuick.Controls

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    // property Message msg: Message {}
    Button {
        id: button
        anchors.centerIn: parent
        text: "Click me"
        onClicked: {
            label.text = MySingleton.exec(label.text)
        }
    }
    Label {
        id: label
        anchors.top: button.bottom
    }
}
// logicmodule/CMakeLists.txt
project(logicmodule VERSION 0.1 LANGUAGES CXX)

qt_add_library(${PROJECT_NAME} STATIC)
qt6_add_qml_module(${PROJECT_NAME}
    URI Main
    VERSION 1.0
    NO_PLUGIN
    RESOURCE_PREFIX "/qt/qml"
    QML_FILES
        Main.qml
    SOURCES
        MySingleton.h
        Message.h
)

target_link_libraries(${PROJECT_NAME} PUBLIC
    Qt6::Core
    Qt6::Gui
    Qt6::Qml
    Qt6::Quick
)

target_include_directories(${PROJECT_NAME}
    PUBLIC
        .
)
// main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "Message.h"
#include "MySingleton.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;

    const QUrl url(u"qrc:/qt/qml/Main/Main.qml"_qs);
    QObject::connect(
        &engine,
        &QQmlApplicationEngine::objectCreated,
        &app,
        [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        },
        Qt::QueuedConnection);

    engine.addImportPath(QCoreApplication::applicationDirPath() + "/qml");
    engine.addImportPath(":/");

    engine.load(url);

    if (engine.rootObjects().isEmpty()) {
        return -1;
    }

    return app.exec();
}
// CMakeLists.txt
cmake_minimum_required(VERSION 3.16)

project(typetest VERSION 0.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)
set(QT_QML_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/qml)

qt_standard_project_setup(REQUIRES 6.5)

add_subdirectory(logicmodule)

qt_add_executable(apptypetest
    main.cpp
)

target_link_libraries(apptypetest PUBLIC
    logicmodule
)

# No effect
# target_include_directories(apptypetest
#   PUBLIC
#       "${PROJECT_SOURCE_DIR}/logicmodule"
#       logicmodule
# )
# include_directories(logicmodule)

It all compiles fine but at runtime I get an error when trying to call MySingleton:

qrc:/qt/qml/Main/Main.qml:16: ReferenceError: MySingleton is not defined

I tried to research this issue but cannot find exactly my case, the closest I got is this one:
qmltyperegistration include path does not acknowledge subdirectories

But the files with custom types seems to be included just fine without any prefixes.

I also noticed that in the build folder I have a file buildDebuglogicmodulelogicmodule_qmltyperegistrations.cpp and it contains calls to qmlRegisterTypesAndRevisions but it seems that it doesn’t have an effect?

Also tried a project structure without any additional libraries (so without logicmodule lib, everything is in the project root and I have only one CMakeLists) and it seem to be working fine and all the custom types are recognized by QML.

Could somebody point me to what am I doing wrong here with the library? Why can’t QML recognize the type when it’s defined inside a lib?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật