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
8\warning By using context properties in your QML code, you create a dependency from your QML code
9 to the specific context you have in mind when writing it. This limits re-usability of your
10 code since the context may be different in other places where it might be used.
11 Furthermore, the dependency is not declared. You never \c import the context or otherwise
12 state what you expect. Therefore, anyone trying to re-use your code will have difficulties
13 finding out whether the place of re-use has a context sufficient for your code.
15\warning Context properties are invisible to any tooling that processes QML code ahead of time,
16 before you load it into the QML engine. The \l{Qt Quick Compiler},
17 \l{qmllint Reference}{qmllint}, and the \l{\QMLLS Reference}{\QMLLS} do
18 not know anything about your context properties and will consider any access to context
19 properties as an \e{unqualified access}.
21\note Context properties can generally be replaced either by regular properties on the root object
22 of a component, or by singletons defined either in C++ using \l{QML_SINGLETON}{QML_SINGLETON}
23 or in QML using \l{Structure of a QML Document#Singleton}{pragma Singleton}.
25When loading a QML object into a C++ application, it can be useful to directly embed some C++ data
26that can be used from within the QML code. This makes it possible, for example, to invoke a C++
27method on the embedded object, or use a C++ object instance as a data model for a QML view.
29The ability to inject C++ data into a QML object is made possible by the QQmlContext class. This
30class exposes data to the context of a QML object so that the data can be referred to directly from
31within the scope of the QML code.
34\section1 Setting a Simple Context Property
36For example, here is a QML item that refers to a \c currentDateTime value that does not exist in
39\snippet qml/qtbinding/context/MyItem.qml 0
41This \c currentDateTime value can be set directly by the C++ application that loads the QML
42component, using QQmlContext::setContextProperty():
44\snippet qml/qtbinding/context/main.cpp 0
46\note Since all expressions evaluated in QML are evaluated in a particular context, if the context
47is modified, all bindings in that context will be re-evaluated. Thus, context properties should be
48used with care outside of application initialization, as this may lead to decreased application
52\section1 Setting an Object as a Context Property
54Context properties can hold either QVariant or QObject* values. This means custom C++ objects can
55also be injected using this approach, and these objects can be modified and read directly in QML.
56Here, we modify the above example to embed a QObject instance instead of a QDateTime value, and the
57QML code invokes a method on the object instance:
63\snippet qml/qtbinding/context-advanced/applicationdata.h 0
65\snippet qml/qtbinding/context-advanced/main.cpp 0
69\snippet qml/qtbinding/context-advanced/MyItem.qml 0
72(Note that date/time values returned from C++ to QML can be formatted through
73\l{QtQml::Qt::formatDateTime}{Qt.formatDateTime()} and associated functions.)
75If the QML item needs to receive signals from the context property, it can connect to them using the
76\l Connections type. For example, if \c ApplicationData has a signal named \c
77dataChanged(), this signal can be connected to using an \c onDataChanged handler within
78a \l Connections object:
80\snippet qml/qtbinding/context-advanced/connections.qml 0
82Context properties can be useful for using C++ based data models in a QML view. See the
85 \li \l {Models and Views: String ListModel Example}{String ListModel}
86 \li \l {Models and Views: Object ListModel Example}{Object ListModel}
87 \li \l {Models and Views: AbstractItemModel Example}{AbstractItemModel}
90demonstrating the use of QStringList, QList<QObject*>-based models and
91QAbstractItemModel in QML views.
93Also see the QQmlContext documentation for more information.