Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
topic.qdoc
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page qtqml-cppintegration-overview.html
6\title Overview - QML and C++ Integration
7\brief Highlights important points about integrating C++ with QML.
8\ingroup explanations-programminglanguages
9
10QML is designed to be easily extensible through C++ code. The classes in the \l {Qt Qml} module
11enable QML objects to be loaded and manipulated from C++, and the nature of QML engine's
12integration with Qt's \l{Meta Object System}{meta object system} enables C++ functionality to be
13invoked directly from QML. This allows the development of hybrid applications which are implemented
14with a mixture of QML, JavaScript and C++ code.
15
16Integrating QML and C++ provides a variety of opportunities, including the ability to:
17
18\list
19\li Separate the user interface code from the application logic code, by implementing the former
20with QML and JavaScript within \l{qtqml-documents-topic.html}{QML documents}, and the latter with
21C++
22\li Use and invoke some C++ functionality from QML (for example, to invoke your application logic,
23use a data model implemented in C++, or call some functions in a third-party C++ library)
24\li Access functionality in the \l {Qt Qml} or \l {Qt Quick} C++ API (for example, to dynamically generate
25images using QQuickImageProvider)
26\li Implement your own \l{qtqml-typesystem-objecttypes.html}{QML object types} from C++
27\unicode{0x2014} whether for use within your own specific application, or for distribution to others
28\endlist
29
30To provide some C++ data or functionality to QML, it must be made available from a QObject-derived
31class. Due to the QML engine's integration with the meta object system, the properties, methods and
32signals of any QObject-derived class are accessible from QML, as described in
33\l{qtqml-cppintegration-exposecppattributes.html}{Exposing Attributes of C++ Types to QML}. Once the
34required functionality is provided by such a class, it can be exposed to QML in a variety of ways:
35
36\list
37\li The class can be
38\l{qtqml-cppintegration-definetypes.html#registering-an-instantiable-object-type}{
39registered as an instantiable QML type}, so that it can be instantiated and used like any ordinary
40\l{qtqml-typesystem-objecttypes.html}{QML object type} from QML code
41\li The class can be registered as a
42\l{qtqml-cppintegration-definetypes.html#registering-singleton-objects-with-a-singleton-type}
43{Singleton Type} so that a single instance of the class may be imported from QML code, allowing the
44instance's properties, methods and signals to be accessed from QML
45\endlist
46
47These are the most common methods of accessing C++ functionality from QML code. The
48\l{qqmlintegration.h}{QML Type Registration Macros} offer a number of other options and variants.
49Additionally, aside from the ability to access C++ functionality from QML, the \l {Qt Qml} module also
50provides ways to do the reverse and manipulate QML objects from C++ code. See
51\l{qtqml-cppintegration-interactqmlfromcpp.html}{Interacting with QML Objects from C++} for more
52details.
53
54It is often desirable to expose some state as global properties to QML.
55\l{qtqml-cppintegration-exposecppstate.html}{Exposing State from C++ to QML}
56describes how to do this.
57
58The C++ code may be integrated into either a C++ application or a standalone QML module,
59depending on whether it is to be distributed as a standalone application or a library. A QML module
60can contain a C++ plugin that can then be dynamically loaded by the QML engine. This allows an
61application to use QML modules that weren't linked into the binary.
62
63You can also manually create standalone plugins. See
64\l{qtqml-modules-cppplugins.html}{Providing Types and Functionality in a C++ Plugin} for more
65information. This is not recommended.
66
67Finally An instance of the class can be \l{qtqml-cppintegration-contextproperties.html}{embedded
68into QML code} as a \e {context property} or \e {context object}, allowing the instance's
69properties, methods and signals to be accessed from QML. Context properties should be avoided.
70
71\section1 Choosing the Correct Integration Method Between C++ and QML
72
73To quickly determine which integration method is appropriate for your situation, the following
74flowchart can be used:
75
76\image cpp-qml-integration-flowchart.svg {Flow chart helps user to select the
77 correct integration}
78
79For a description of the macros in the flowchart, see the
80\l {qtqml-cppintegration-definetypes.html}{Defining QML Types from C++} documentation.
81
82\section1 Exposing Attributes of C++ Classes to QML
83
84QML can easily be extended from C++ due to the QML engine's integration with the Qt meta object
85system. This integration allows the properties, methods and signals of any QObject-derived class to
86be accessible from QML: properties can be read and modified, methods can be invoked from JavaScript
87expressions and signal handlers are automatically created for signals as necessary. Additionally,
88enumeration values of a QObject-derived class are accessible from QML.
89
90See \l{qtqml-cppintegration-exposecppattributes.html}{Exposing Attributes of C++ Types to QML} for
91more information.
92
93
94\section1 Defining QML Types from C++
95
96QML types can be defined in C++ and then registered with the \l{qtqml-typesystem-topic.html}{QML
97type system}. This allows a C++ class to be instantiated as a \l {QML Object Types}{QML object type}, enabling custom
98object types to be implemented in C++ and integrated into existing QML code. A C++ class may be also
99registered for other purposes: for example, it could be registered as a \e {Singleton Type} to enable a
100single class instance to be imported by QML code, or it could be registered to enable the
101enumeration values of a non-instantiable class to be accessible from QML.
102
103Additionally, the \l {Qt Qml} module provides mechanisms to define QML types that integrate with QML
104concepts like attached properties and default properties.
105
106For more information on registering and creating custom QML types from C++, see the \l
107{qtqml-cppintegration-definetypes.html}{Defining QML Types from C++} documentation.
108
109
110\section1 Embedding C++ Objects into QML with Context Properties
111
112C++ objects and values can be embedded directly into the context (or \e scope) of loaded QML objects
113using \e {context properties} and \e {context objects}. This is achieved through the QQmlContext
114class provided by the \l {Qt Qml} module, which exposes data to the context of a QML component, allowing
115data to be injected from C++ into QML.
116
117See \l{qtqml-cppintegration-contextproperties.html}{Embedding C++ Objects into QML with Context
118Properties} for more information.
119
120
121\section1 Interacting with QML Objects from C++
122
123QML object types can be instantiated from C++ and inspected in order to access their properties,
124invoke their methods and receive their signal notifications. This is possible due to the fact that
125all QML object types are implemented using QObject-derived classes, enabling the QML engine to
126dynamically load and introspect objects through the Qt meta object system.
127
128\include warning.qdocinc
129
130For more information on accessing QML objects from C++, see the documentation on
131\l{qtqml-cppintegration-interactqmlfromcpp.html}{Interacting with QML Objects from C++},
132and the \l {Exposing Data from C++ to QML} section of the Best Practices page.
133
134
135\section1 Data Type Conversion Between QML and C++
136
137When data values are exchanged between QML and C++, they are converted by the QML engine to have the
138correct data types as appropriate for use from QML or C++, providing the data types involved are
139known to the engine.
140
141See \l{qtqml-cppintegration-data.html}{Data Type Conversion Between QML and C++} for information on
142the built-in types supported by the engine and how these types are converted for use when exchanged
143between QML and C++.
144
145*/