1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
4\page qtqml-cppintegration-contextproperties.html
5\title Embedding C++ Objects into QML with Context Properties
6\brief Description of how to embed C++ data into QML using context properties
8When loading a QML object into a C++ application, it can be useful to directly embed some C++ data
9that can be used from within the QML code. This makes it possible, for example, to invoke a C++
10method on the embedded object, or use a C++ object instance as a data model for a QML view.
12The ability to inject C++ data into a QML object is made possible by the QQmlContext class. This
13class exposes data to the context of a QML object so that the data can be referred to directly from
14within the scope of the QML code.
17\section1 Setting a Simple Context Property
19For example, here is a QML item that refers to a \c currentDateTime value that does not exist in
22\snippet qml/qtbinding/context/MyItem.qml 0
24This \c currentDateTime value can be set directly by the C++ application that loads the QML
25component, using QQmlContext::setContextProperty():
27\snippet qml/qtbinding/context/main.cpp 0
29\note Since all expressions evaluated in QML are evaluated in a particular context, if the context
30is modified, all bindings in that context will be re-evaluated. Thus, context properties should be
31used with care outside of application initialization, as this may lead to decreased application
35\section1 Setting an Object as a Context Property
37Context properties can hold either QVariant or QObject* values. This means custom C++ objects can
38also be injected using this approach, and these objects can be modified and read directly in QML.
39Here, we modify the above example to embed a QObject instance instead of a QDateTime value, and the
40QML code invokes a method on the object instance:
46\snippet qml/qtbinding/context-advanced/applicationdata.h 0
48\snippet qml/qtbinding/context-advanced/main.cpp 0
52\snippet qml/qtbinding/context-advanced/MyItem.qml 0
55(Note that date/time values returned from C++ to QML can be formatted through
56\l{QtQml::Qt::formatDateTime}{Qt.formatDateTime()} and associated functions.)
58If the QML item needs to receive signals from the context property, it can connect to them using the
59\l Connections type. For example, if \c ApplicationData has a signal named \c
60dataChanged(), this signal can be connected to using an \c onDataChanged handler within
61a \l Connections object:
63\snippet qml/qtbinding/context-advanced/connections.qml 0
65Context properties can be useful for using C++ based data models in a QML view. See the
68 \li \l {Models and Views: String ListModel Example}{String ListModel}
69 \li \l {Models and Views: Object ListModel Example}{Object ListModel}
70 \li \l {Models and Views: AbstractItemModel Example}{AbstractItemModel}
73demonstrating the use of QStringList, QList<QObject*>-based models and
74QAbstractItemModel in QML views.
76Also see the QQmlContext documentation for more information.