// Copyright (C) 2017 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only /*! \page qtqml-cppintegration-contextproperties.html \title Embedding C++ Objects into QML with Context Properties \brief Description of how to embed C++ data into QML using context properties \warning By using context properties in your QML code, you create a dependency from your QML code to the specific context you have in mind when writing it. This limits re-usability of your code since the context may be different in other places where it might be used. Furthermore, the dependency is not declared. You never \c import the context or otherwise state what you expect. Therefore, anyone trying to re-use your code will have difficulties finding out whether the place of re-use has a context sufficient for your code. \warning Context properties are invisible to any tooling that processes QML code ahead of time, before you load it into the QML engine. The \l{Qt Quick Compiler}, \l{qmllint Reference}{qmllint}, and the \l{\QMLLS Reference}{\QMLLS} do not know anything about your context properties and will consider any access to context properties as an \e{unqualified access}. \note Context properties can generally be replaced either by regular properties on the root object of a component, or by singletons defined either in C++ using \l{QML_SINGLETON}{QML_SINGLETON} or in QML using \l{Structure of a QML Document#Singleton}{pragma Singleton}. When loading a QML object into a C++ application, it can be useful to directly embed some C++ data that can be used from within the QML code. This makes it possible, for example, to invoke a C++ method on the embedded object, or use a C++ object instance as a data model for a QML view. The ability to inject C++ data into a QML object is made possible by the QQmlContext class. This class exposes data to the context of a QML object so that the data can be referred to directly from within the scope of the QML code. \section1 Setting a Simple Context Property For example, here is a QML item that refers to a \c currentDateTime value that does not exist in the current scope: \snippet qml/qtbinding/context/MyItem.qml 0 This \c currentDateTime value can be set directly by the C++ application that loads the QML component, using QQmlContext::setContextProperty(): \snippet qml/qtbinding/context/main.cpp 0 \note Since all expressions evaluated in QML are evaluated in a particular context, if the context is modified, all bindings in that context will be re-evaluated. Thus, context properties should be used with care outside of application initialization, as this may lead to decreased application performance. \section1 Setting an Object as a Context Property Context properties can hold either QVariant or QObject* values. This means custom C++ objects can also be injected using this approach, and these objects can be modified and read directly in QML. Here, we modify the above example to embed a QObject instance instead of a QDateTime value, and the QML code invokes a method on the object instance: \table \row \li C++ \li \snippet qml/qtbinding/context-advanced/applicationdata.h 0 \codeline \snippet qml/qtbinding/context-advanced/main.cpp 0 \row \li QML \li \snippet qml/qtbinding/context-advanced/MyItem.qml 0 \endtable (Note that date/time values returned from C++ to QML can be formatted through \l{QtQml::Qt::formatDateTime}{Qt.formatDateTime()} and associated functions.) If the QML item needs to receive signals from the context property, it can connect to them using the \l Connections type. For example, if \c ApplicationData has a signal named \c dataChanged(), this signal can be connected to using an \c onDataChanged handler within a \l Connections object: \snippet qml/qtbinding/context-advanced/connections.qml 0 Context properties can be useful for using C++ based data models in a QML view. See the following examples: \list \li \l {Models and Views: String ListModel Example}{String ListModel} \li \l {Models and Views: Object ListModel Example}{Object ListModel} \li \l {Models and Views: AbstractItemModel Example}{AbstractItemModel} \endlist demonstrating the use of QStringList, QList-based models and QAbstractItemModel in QML views. Also see the QQmlContext documentation for more information. */