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. How to create a custom QML item on Qt6+CMake ?
Qt 6.11 is out! See what's new in the release blog

How to create a custom QML item on Qt6+CMake ?

Scheduled Pinned Locked Moved Unsolved Qt 6
2 Posts 1 Posters 906 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.
  • R Offline
    R Offline
    r3d9u11
    wrote on last edited by r3d9u11
    #1

    Hello. I'm trying to create some simple custom Qt6 item with C++ /CMake for QML.
    But QML doesn't see my new item.
    Could somebody explain, what's wrong?

    the folder of item is "lib/MyCustomQmlItem"

    lib/MyCustomQmlItem/include/MyCustomQmlItem.hpp

    #include <QQuickItem>
    class MyCustomQmlItem: public QQuickItem
    {
        Q_OBJECT
        QML_ELEMENT
    public:
        explicit MyCustomQmlItem(QQuickItem* parent = 0);
    };
    

    lib/MyCustomQmlItem/src/MyCustomQmlItem.cpp

    #include "MyCustomQmlItem.hpp"
    MyCustomQmlItem::MyCustomQmlItem(QQuickItem* parent):
        QQuickItem{parent}
    {
        qDebug() << "Hello from MyCustomQmlItem";
    }
    

    lib/MyCustomQmlItem/CMakeLists.txt

    project(MyCustomQmlItem)
    
    SET(CMAKE_AUTOMOC ON)
    SET(CMAKE_AUTOUIC ON)
    SET(CMAKE_AUTORCC ON)
    
    add_library(MyCustomQmlItem "src/MyCustomQmlItem.cpp" "include/MyCustomQmlItem.hpp")
    
    qt_add_qml_module(
      MyCustomQmlItem
      URI example.MyCustomQmlItem
      VERSION 1.0
      SOURCES
        "src/MyCustomQmlItem.cpp"
        "include/MyCustomQmlItem.hpp")
    
    target_include_directories(MyCustomQmlItem PUBLIC "include")
    
    find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Quick REQUIRED)
    
    target_compile_definitions(MyCustomQmlItem
      PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
    target_link_libraries(MyCustomQmlItem
      PRIVATE Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Quick)
    

    main CMakeLists.txt:

    ...
    SET(CMAKE_AUTOMOC ON)
    SET(CMAKE_AUTOUIC ON)
    SET(CMAKE_AUTORCC ON)
    ...
    qt_add_executable(${OUTPUT_NAME} ${PROJECT_SOURCES} )
    qt_add_qml_module(${OUTPUT_NAME} URI "main" VERSION 1.0 QML_FILES "main.qml")
    ...
    add_subdirectory("lib/MyCustomQmlItem")
    target_link_libraries(${OUTPUT} MyCustomQmlItem)
    include_directories("lib/MyCustomQmlItem/include")
    

    is it right usage of qt_add_qml_module in my cmake ?

    maybe somebody has an example of custom module for Qt6 + CMake (all examples what I found only for Qt5 and(or) Qbs).

    Thanks!

    1 Reply Last reply
    0
    • R Offline
      R Offline
      r3d9u11
      wrote on last edited by
      #2

      Ok. I've found example of CMakeLists.txt in Qt6 repo.

      it works if apply all sources of sub-item to main executable
      like

      qt_add_executable(chapter1-basics
          main.cpp piechart.cpp piechart.h
      )
      

      but is there way to separate sub-item as external library ?
      something like a

      qt_add_executable(chapter1-basics
          main.cpp
      )
      ...
      qt_add_library(lib_piechart piechart.cpp piechart.h)
      target_link_libraries(lib_piechart PUBLIC Qt::Core Qt::Gui Qt::Qml Qt::Quick)
      target_link_libraries(chapter1-basics PRIVATE lib_piechart)
      
      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