How to create a custom QML item on Qt6+CMake ?
Unsolved
Qt 6
-
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!
-
Ok. I've found example of CMakeLists.txt in Qt6 repo.
it works if apply all sources of sub-item to main executable
likeqt_add_executable(chapter1-basics main.cpp piechart.cpp piechart.h )
but is there way to separate sub-item as external library ?
something like aqt_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)