1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
5\page qtqml-cppintegration-overview.html
6\title Overview - QML and C++ Integration
7\brief Highlights important points about integrating C++ with QML.
9QML is designed to be easily extensible through C++ code. The classes in the \l {Qt QML} module
10enable QML objects to be loaded and manipulated from C++, and the nature of QML engine's
11integration with Qt's \l{Meta Object System}{meta object system} enables C++ functionality to be
12invoked directly from QML. This allows the development of hybrid applications which are implemented
13with a mixture of QML, JavaScript and C++ code.
15Integrating QML and C++ provides a variety of opportunities, including the ability to:
18\li Separate the user interface code from the application logic code, by implementing the former
19with QML and JavaScript within \l{qtqml-documents-topic.html}{QML documents}, and the latter with
21\li Use and invoke some C++ functionality from QML (for example, to invoke your application logic,
22use a data model implemented in C++, or call some functions in a third-party C++ library)
23\li Access functionality in the \l {Qt QML} or \l {Qt Quick} C++ API (for example, to dynamically generate
24images using QQuickImageProvider)
25\li Implement your own \l{qtqml-typesystem-objecttypes.html}{QML object types} from C++
26\unicode{0x2014} whether for use within your own specific application, or for distribution to others
29To provide some C++ data or functionality to QML, it must be made available from a QObject-derived
30class. Due to the QML engine's integration with the meta object system, the properties, methods and
31signals of any QObject-derived class are accessible from QML, as described in
32\l{qtqml-cppintegration-exposecppattributes.html}{Exposing Attributes of C++ Types to QML}. Once the
33required functionality is provided by such a class, it can be exposed to QML in a variety of ways:
37\l{qtqml-cppintegration-definetypes.html#registering-an-instantiable-object-type}{
38registered as an instantiable QML type}, so that it can be instantiated and used like any ordinary
39\l{qtqml-typesystem-objecttypes.html}{QML object type} from QML code
40\li The class can be registered as a
41\l{qtqml-cppintegration-definetypes.html#registering-singleton-objects-with-a-singleton-type}
42{Singleton Type} so that a single instance of the class may be imported from QML code, allowing the
43instance's properties, methods and signals to be accessed from QML
44\li An instance of the class can be \l{qtqml-cppintegration-contextproperties.html}{embedded into
45QML code} as a \e {context property} or \e {context object}, allowing the instance's properties,
46methods and signals to be accessed from QML
49These are the most common methods of accessing C++ functionality from QML code; for more options and
50details, see the main documentation pages that are described in the sections further below.
51Additionally, aside from the ability to access C++ functionality from QML, the \l {Qt QML} module also
52provides ways to do the reverse and manipulate QML objects from C++ code. See
53\l{qtqml-cppintegration-interactqmlfromcpp.html}{Interacting with QML Objects from C++} for more
56It is often desirable to expose some state as global properties to QML.
57\l{qtqml-cpp-integration-exposecppstate.html}{Exposing State from C++ to QML}
58describes how to do this.
60Finally, the C++ code may be integrated into either a C++ application or a C++ plugin depending on
61whether it is to be distributed as a standalone application or a library. A plugin can be integrated
62with a QML module that can then be imported and used by QML code in other applications; see
63\l{qtqml-modules-cppplugins.html}{Providing Types and Functionality in a C++ Plugin} for more
66\section1 Choosing the Correct Integration Method Between C++ and QML
68To quickly determine which integration method is appropriate for your situation, the following
71\image cpp-qml-integration-flowchart.png
73For a description of the macros in the flowchart, see the
74\l {qtqml-cppintegration-definetypes.html}{Defining QML Types from C++} documentation.
76\section1 Exposing Attributes of C++ Classes to QML
78QML can easily be extended from C++ due to the QML engine's integration with the Qt meta object
79system. This integration allows the properties, methods and signals of any QObject-derived class to
80be accessible from QML: properties can be read and modified, methods can be invoked from JavaScript
81expressions and signal handlers are automatically created for signals as necessary. Additionally,
82enumeration values of a QObject-derived class are accessible from QML.
84See \l{qtqml-cppintegration-exposecppattributes.html}{Exposing Attributes of C++ Types to QML} for
88\section1 Defining QML Types from C++
90QML types can be defined in C++ and then registered with the \l{qtqml-typesystem-topic.html}{QML
91type system}. This allows a C++ class to be instantiated as a \l {QML Object Types}{QML object type}, enabling custom
92object types to be implemented in C++ and integrated into existing QML code. A C++ class may be also
93registered for other purposes: for example, it could be registered as a \e {Singleton Type} to enable a
94single class instance to be imported by QML code, or it could be registered to enable the
95enumeration values of a non-instantiable class to be accessible from QML.
97Additionally, the \l {Qt QML} module provides mechanisms to define QML types that integrate with QML
98concepts like attached properties and default properties.
100For more information on registering and creating custom QML types from C++, see the \l
101{qtqml-cppintegration-definetypes.html}{Defining QML Types from C++} documentation.
104\section1 Embedding C++ Objects into QML with Context Properties
106C++ objects and values can be embedded directly into the context (or \e scope) of loaded QML objects
107using \e {context properties} and \e {context objects}. This is achieved through the QQmlContext
108class provided by the \l {Qt QML} module, which exposes data to the context of a QML component, allowing
109data to be injected from C++ into QML.
111See \l{qtqml-cppintegration-contextproperties.html}{Embedding C++ Objects into QML with Context
112Properties} for more information.
115\section1 Interacting with QML Objects from C++
117QML object types can be instantiated from C++ and inspected in order to access their properties,
118invoke their methods and receive their signal notifications. This is possible due to the fact that
119all QML object types are implemented using QObject-derived classes, enabling the QML engine to
120dynamically load and introspect objects through the Qt meta object system.
122\include warning.qdocinc
124For more information on accessing QML objects from C++, see the documentation on
125\l{qtqml-cppintegration-interactqmlfromcpp.html}{Interacting with QML Objects from C++}.
128\section1 Data Type Conversion Between QML and C++
130When data values are exchanged between QML and C++, they are converted by the QML engine to have the
131correct data types as appropriate for use from QML or C++, providing the data types involved are
134See \l{qtqml-cppintegration-data.html}{Data Type Conversion Between QML and C++} for information on
135the built-in types supported by the engine and how these types are converted for use when exchanged