Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qt_generate_foreign_qml_types.qdoc
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page qt-generate-foreign-qml-types.html
6\ingroup cmake-commands-qtqml
7
8\title qt_generate_foreign_qml_types
9\target qt6_generate_foreign_qml_types
10
11\summary{Registers types from one target in a QML module.}
12
13\include cmake-find-package-qml.qdocinc
14
15\section1 Synopsis
16
17\badcode
18qt_generate_foreign_qml_types(
19 source_target
20 destination_qml_target
21)
22
23\endcode
24
25\versionlessCMakeCommandsNote qt6_generate_foreign_qml_types()
26
27\section1 Description
28
29\c qt_generate_foreign_qml_types extracts types marked via QML registration
30macros (like \l QML_ELEMENT) from \c source_target and registers them as foreign
31types in the QML module \c destination_qml_target.
32
33This can be useful when one wants to create a library with optional QML integration, without
34depending directly on QML.
35
36\badcode
37// myclass.h
38#include <QtQmlIntegration/qqmlintegration.h>
39
40class MyClass : public QObject
41{
42 QML_ELEMENT
43 Q_OBJECT
44
45 // [...]
46};
47\endcode
48
49\badcode
50# CMakeLists.txt
51qt_add_library(mylib myclass.h ...)
52target_link_libraries(mylib PRIVATE Qt::Core Qt::QmlIntegration)
53
54qt_add_qml_module(mylib_declarative
55 VERSION 1.0
56 URI "mylib"
57 ...
58)
59qt_generate_foreign_qml_types(mylib mylib_declarative)
60\endcode
61
62\note In the example above, \c mylib does not depend on QtQml or QtQuick, but only on the
63header-only QmlIntegration target (for the QtQmlIntegration/qqmlintegration.h header, which provides
64the \c QML_ELEMENT macro).
65
66The effect is equivalent to using \c QML_FOREIGN with custom structs in the QML library to expose
67the types.
68
69\note In order to implement custom behavior, such as exposing an existing
70singleton instance with its own life cycle to QML, you should add custom types
71to your QML library (mylib_declarative in the above example). In turn, you
72should omit the \l QML_ELEMENT and similar macros from the original C++ classes
73so that qt_generate_foreign_qml_types() does not generate more QML integration
74structs for them. The QML macros, as well as any singleton factory functions,
75can be added to the structs that contain the \l QML_FOREIGN.
76
77*/