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. Qt new CMake API + qmllint + qt_add_qml_module - how to setup
Forum Updated to NodeBB v4.3 + New Features

Qt new CMake API + qmllint + qt_add_qml_module - how to setup

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 374 Views 1 Watching
  • 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.
  • Matthew11M Offline
    Matthew11M Offline
    Matthew11
    wrote on last edited by
    #1

    So I switched my project to Qt 6.8 qt_add_qml_module() API, and wanted to try out the qmllint and qmlformat. I have dummy basic project:

    C:.
        CMakeLists.txt
        main.cpp
        Main.qml
        MyButton.qml
    

    The CMakeLists.txt:

    cmake_minimum_required(VERSION 3.20)
    
    project(qml-sample VERSION 0.1 LANGUAGES CXX)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    find_package(Qt6 6.8 REQUIRED COMPONENTS Quick)
    
    qt_standard_project_setup(REQUIRES 6.8)
    
    qt_add_executable(qml-sample main.cpp)
    qt_add_qml_module(qml-sample
        URI AppMain
        VERSION 1.0
        QML_FILES
            Main.qml
            MyButton.qml
    )
    
    target_link_libraries(qml-sample PRIVATE Qt6::Quick)
    

    The main.cpp:

    #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.loadFromModule("AppMain", "Main");
    
        return app.exec();
    }
    

    The Main.qml:

    import QtQuick 2.15
    import QtQuick.Controls 2.15
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        MyButton {
            anchors.centerIn: parent
            width: 100
            height: 50
        }
    }
    
    

    Add dummy control MyButton.qml:

    import QtQuick 2.15
    import QtQuick.Controls 2.15
    import QtQuick.Layouts 2.15
    
    Rectangle {
        Button {
            text: "Click me!"
            anchors.fill: parent
        }
    }
    

    And that works fine, when I call qmllint target qml-sample_qmllint by hand:

    Info: C:/sandbox/qml-test/Main.qml:2:1: Unused import [unused-imports]
    import QtQuick.Controls 2.15
    ^^^^^^
    Info: C:/sandbox/qml-test/MyButton.qml:3:1: Unused import [unused-imports]
    import QtQuick.Layouts 2.15
    ^^^^^^
    

    and in QtCreator I can see that issues are reported too:

    efd6ebd0-f918-4ec3-bbb3-cc06ffd38fbe-image.png

    Now I wanted to modularize things, and made two separate QML modules - one with Main.qml the AppMain and one with my controls Controls, and link them to main application, so now structure looks like:

    C:.
    │   CMakeLists.txt
    │   main.cpp
    │   Main.qml
    │
    └───controls
            CMakeLists.txt
            MyButton.qml
    

    So controls/CMakeLists.txt:

    qt_add_qml_module(controls STATIC NO_PLUGIN
        URI Controls
        VERSION 1.0
        QML_FILES
            MyButton.qml
        OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/Controls
    )
    

    And main CMakeLists.txt changes:

    --- a/CMakeLists.txt
    +++ b/CMakeLists.txt
    @@ -6,13 +6,15 @@ find_package(Qt6 6.8 REQUIRED COMPONENTS Quick)
     
     qt_standard_project_setup(REQUIRES 6.8)
     
    +add_subdirectory(controls)
    +
     qt_add_executable(qml-sample main.cpp)
    -qt_add_qml_module(qml-sample
    +qt_add_qml_module(app-main STATIC NO_PLUGIN
         URI AppMain
         VERSION 1.0
         QML_FILES
             Main.qml
    -        MyButton.qml
    +    OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/AppMain
     )
     
    -target_link_libraries(qml-sample PRIVATE Qt6::Quick)
    +target_link_libraries(qml-sample PRIVATE Qt6::Quick app-main controls)
    

    And project still compiles and works fine, but qmllint is not working - i get many errors when calling qmllint targets for app-main:

    Warning: C:/sandbox/qml-test/Main.qml:10:5: MyButton was not found. Did you add all imports and dependencies? [import]
        MyButton {
        ^^^^^^^^
    Info: Did you mean "Button"?
        Button {
        ^^^^^^
    Warning: C:/sandbox/qml-test/Main.qml:11:9: unknown grouped property scope anchors. [unqualified]
            anchors.centerIn: parent
            ^^^^^^^
    Warning: C:/sandbox/qml-test/Main.qml:10:5: 'MyButton' is used but it is not resolved [unresolved-type]
        MyButton {
        ^^^^^^^^
    Warning: C:/sandbox/qml-test/Main.qml:11:9: 'anchors' is used but it is not resolved [unresolved-type]
            anchors.centerIn: parent
            ^^^^^^^
    ...
    

    QtCreator is also confused:

    9cea5a47-7899-4436-aaba-6d4e555900be-image.png

    What I'm missing or not doing? Any help appreciated :)

    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