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.