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. Qt 6.2 + CMake + extending QML with a C++ class
Forum Updated to NodeBB v4.3 + New Features

Qt 6.2 + CMake + extending QML with a C++ class

Scheduled Pinned Locked Moved Solved Qt 6
qt6.2c++qmlcmake
21 Posts 3 Posters 6.0k Views 2 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.
  • P Offline
    P Offline
    paulmasri
    wrote on 19 Jan 2022, 12:25 last edited by
    #21

    So for clarity for anyone with the same question, the following CMakeLists.txt makes the class Person available to QML via import People, by packaging it behind the scenes as Peopleplugin.

    cmake_minimum_required(VERSION 3.16)
    
    project(MyProject VERSION 0.1 LANGUAGES CXX)
    
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    find_package(Qt6 6.2 COMPONENTS Quick REQUIRED)
    
    qt_add_executable(appMyProject
        main.cpp
    )
    
    qt_add_qml_module(appMyProject
        URI MyProject
        VERSION 1.0
        QML_FILES main.qml
    )
    qt_add_qml_module(People
        URI People
        VERSION 1.0
        SOURCES Person.h Person.cpp
    )
    
    target_include_directories(Peopleplugin
         PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
    )
    
    target_compile_definitions(appMyProject
        PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
    target_link_libraries(appMyProject
        PRIVATE Qt6::Quick People)
    

    and for those eagle-eyed, main.qml was buggy and should be:

    import QtQuick
    import People
    
    Window {
        width: 640
        height: 480
        visible: true
    
        Text {
            text: personBob.name + " wears size " + personBob.shoeSize + " shoes."
        }
    
        Person {
            id: personBob
            name: "Bob Jones"
            shoeSize: 12
        }
    }
    

    The C++ class Person is derived from QObject:

    #include <QObject>
    #include <QtQml/qqml.h>
    
    class Person : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
        Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize NOTIFY shoeSizeChanged)
        QML_ELEMENT
    ...
    

    main.cpp is as autogenerated when creating a new Qt Quick 6.2 project in Qt Creator and makes no mention of Person or the People plugin.

    Other C++ classes can #include "Person.h" and make use of the Person class.

    1 Reply Last reply
    2

    21/21

    19 Jan 2022, 12:25

    • Login

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