Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. Building reusable Qt6 Qml Module with CMake
Forum Updated to NodeBB v4.3 + New Features

Building reusable Qt6 Qml Module with CMake

Scheduled Pinned Locked Moved Unsolved Qt 6
qmlqt6modules
1 Posts 1 Posters 572 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.
  • S Offline
    S Offline
    sadeq
    wrote on last edited by
    #1

    Hello
    So I've been trying to create a reusable module that I can use across my apps.
    the module is structred like that

    ├── CMakeLists.txt
    └── examples
        ├── CMakeLists.txt
        └── demo
            ├── CMakeLists.txt
            ├── main.cpp
            ├── main.qml
    └── src
            ├── CMakeLists.txt
            ├── Extra.qml
    

    top level CMakeLists

    cmake_minimum_required(VERSION 3.16)
    project(mymodule VERSION 0.1 LANGUAGES CXX)
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    find_package(Qt6 6.2 COMPONENTS Quick REQUIRED)
    
    add_subdirectory(src)
    add_subdirectory(examples)
    

    src's CMakeLists

    qt_add_library(mymodule STATIC)
    qt_add_qml_module(mymodule
        URI MyModule
        VERSION 1.0
        RESOURCE_PREFIX "/qt-project.org/imports"
        IMPORT_PATH "/qt-project.org/imports"
        QML_FILES
            Extra.qml
    )
    

    examples CMakeLists

    add_subdirectory(demo)
    

    demo's CMakeLists

    cmake_minimum_required(VERSION 3.14)
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    set(CMAKE_AUTOUIC ON)
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTORCC ON)
    
    set(CMAKE_CXX_STANDARD 17)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS  Core Quick)
    find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Quick)
    
    qt_add_executable(appdemo
        main.cpp
    )
    
    qt_add_qml_module(appdemo
        URI Demo
        VERSION 1.0
        IMPORTS MyModule
        QML_FILES main.qml
    )
    
    set_target_properties(appdemo PROPERTIES
        MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
        MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
        MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
        MACOSX_BUNDLE TRUE
        WIN32_EXECUTABLE TRUE
    )
    
    target_compile_definitions(appdemo
        PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
    
    target_link_libraries(appdemo
        PRIVATE Qt6::Quick mymoduleplugin)
    

    demo's main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QDebug>
    #include <QtQml/qqmlextensionplugin.h>
    Q_IMPORT_QML_PLUGIN(MyModulePlugin)
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        qDebug()<<engine.importPathList();
        const QUrl url(u"qrc:/Demo/main.qml"_qs);
        qDebug()<<engine.importPathList();
    
        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();
    }
    

    demo's main.qml

    import QtQuick
    import QtQuick.Window
    import QtQuick.Controls
    import MyModule
    
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: qsTr("Demo")
        visibility: Window.Maximized
    
        Extra{
    
        }
    }
    

    now everything compiles and works fine,
    except that qtcreator complains that MyModule is not found

    QML module not found (MyModule).
    

    what I'm I missing here?
    please note that if "src" where inside the demo's directory and included it with add_subdirectory(src) it would work fine, but this will mess with the directory strucuture.

    you can download the project from this link

    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