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
contextproperties.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\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
7
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.
14
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}.
20
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}.
24
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.
28
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.
32
33
34\section1 Setting a Simple Context Property
35
36For example, here is a QML item that refers to a \c currentDateTime value that does not exist in
37the current scope:
38
39\snippet qml/qtbinding/context/MyItem.qml 0
40
41This \c currentDateTime value can be set directly by the C++ application that loads the QML
42component, using QQmlContext::setContextProperty():
43
44\snippet qml/qtbinding/context/main.cpp 0
45
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
49performance.
50
51
52\section1 Setting an Object as a Context Property
53
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:
58
59\table
60\row
61\li C++
62\li
63\snippet qml/qtbinding/context-advanced/applicationdata.h 0
64\codeline
65\snippet qml/qtbinding/context-advanced/main.cpp 0
66\row
67\li QML
68\li
69\snippet qml/qtbinding/context-advanced/MyItem.qml 0
70\endtable
71
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.)
74
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:
79
80\snippet qml/qtbinding/context-advanced/connections.qml 0
81
82Context properties can be useful for using C++ based data models in a QML view. See the
83following examples:
84\list
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}
88\endlist
89
90demonstrating the use of QStringList, QList<QObject*>-based models and
91QAbstractItemModel in QML views.
92
93Also see the QQmlContext documentation for more information.
94
95*/