QQmlContext¶
The
QQmlContextclass defines a context within a QML engine. More…

Synopsis¶
Functions¶
def
baseUrl()def
contextObject()def
contextProperty(arg__1)def
engine()def
isValid()def
nameForObject(arg__1)def
parentContext()def
resolvedUrl(arg__1)def
setBaseUrl(arg__1)def
setContextObject(arg__1)def
setContextProperty(arg__1, arg__2)def
setContextProperty(arg__1, arg__2)
Detailed Description¶
Contexts allow data to be exposed to the QML components instantiated by the QML engine.
Each
QQmlContextcontains a set of properties, distinct from itsQObjectproperties, that allow data to be explicitly bound to a context by name. The context properties are defined and updated by callingsetContextProperty(). The following example shows a Qt model being bound to a context and then accessed from a QML file.QQmlEngine engine; QStringListModel modelData; QQmlContext *context = new QQmlContext(engine.rootContext()); context->setContextProperty("myModel", &modelData); QQmlComponent component(&engine); component.setData("import QtQuick 2.0; ListView { model: myModel }", QUrl()); QObject *window = component.create(context);Note
It is the responsibility of the creator to delete any
QQmlContextit constructs. If thecontextobject in the example is no longer needed when thewindowcomponent instance is destroyed, thecontextmust be destroyed explicitly. The simplest way to ensure this is to setwindowas the parent ofcontext.To simplify binding and maintaining larger data sets, a context object can be set on a
QQmlContext. All the properties of the context object are available by name in the context, as though they were all individually added through calls tosetContextProperty(). Changes to the property’s values are detected through the property’s notify signal. Setting a context object is both faster and easier than manually adding and maintaining context property values.The following example has the same effect as the previous one, but it uses a context object.
class MyDataSet : public QObject { // ... Q_PROPERTY(QAbstractItemModel *myModel READ model NOTIFY modelChanged) // ... }; MyDataSet myDataSet; QQmlEngine engine; QQmlContext *context = new QQmlContext(engine.rootContext()); context->setContextObject(&myDataSet); QQmlComponent component(&engine); component.setData("import QtQuick 2.0; ListView { model: myModel }", QUrl()); component.create(context);All properties added explicitly by
setContextProperty()take precedence over the context object’s properties.
The Context Hierarchy¶
Contexts form a hierarchy. The root of this hierarchy is the QML engine’s
root context. Child contexts inherit the context properties of their parents; if a child context sets a context property that already exists in its parent, the new context property overrides that of the parent.The following example defines two contexts -
context1andcontext2. The second context overrides the “b” context property inherited from the first with a new value.QQmlEngine engine; QQmlContext *context1 = new QQmlContext(engine.rootContext()); QQmlContext *context2 = new QQmlContext(context1); context1->setContextProperty("a", 9001); context1->setContextProperty("b", 9001); context2->setContextProperty("b", 42);While QML objects instantiated in a context are not strictly owned by that context, their bindings are. If a context is destroyed, the property bindings of outstanding QML objects will stop evaluating.
Warning
Setting the context object or adding new context properties after an object has been created in that context is an expensive operation (essentially forcing all bindings to reevaluate). Thus whenever possible you should complete “setup” of the context before using it to create any objects.
See also
Exposing Attributes of C++ Types to QML
- class PySide2.QtQml.QQmlContext(parent[, objParent=None])¶
PySide2.QtQml.QQmlContext(parent[, objParent=None])
- param parent:
- param objParent:
Create a new
QQmlContextwith the givenparentContext, and theQObjectparent.Create a new
QQmlContextas a child ofengine‘s root context, and theQObjectparent.
- PySide2.QtQml.QQmlContext.baseUrl()¶
- Return type:
Returns the base url of the component, or the containing component if none is set.
See also
- PySide2.QtQml.QQmlContext.contextObject()¶
- Return type:
Return the context object, or
Noneif there is no context object.See also
- PySide2.QtQml.QQmlContext.contextProperty(arg__1)¶
- Parameters:
arg__1 – str
- Return type:
object
Returns the value of the
nameproperty for this context as aQVariant.See also
- PySide2.QtQml.QQmlContext.engine()¶
- Return type:
Return the context’s
QQmlEngine, orNoneif the context has noQQmlEngineor theQQmlEnginewas destroyed.
- PySide2.QtQml.QQmlContext.isValid()¶
- Return type:
bool
Returns whether the context is valid.
To be valid, a context must have a engine, and it’s
contextObject(), if any, must not have been deleted.
- PySide2.QtQml.QQmlContext.nameForObject(arg__1)¶
- Parameters:
arg__1 –
PySide2.QtCore.QObject- Return type:
str
Returns the name of
objectin this context, or an empty string ifobjectis not named in the context. Objects are named bysetContextProperty(), or by ids in the case of QML created contexts.If the object has multiple names, the first is returned.
- PySide2.QtQml.QQmlContext.parentContext()¶
- Return type:
Return the context’s parent
QQmlContext, orNoneif this context has no parent or if the parent has been destroyed.
- PySide2.QtQml.QQmlContext.resolvedUrl(arg__1)¶
- Parameters:
arg__1 –
PySide2.QtCore.QUrl- Return type:
Resolves the URL
srcrelative to the URL of the containing component.See also
- PySide2.QtQml.QQmlContext.setBaseUrl(arg__1)¶
- Parameters:
arg__1 –
PySide2.QtCore.QUrl
Explicitly sets the url
resolvedUrl()will use for relative references tobaseUrl.Calling this function will override the url of the containing component used by default.
See also
- PySide2.QtQml.QQmlContext.setContextObject(arg__1)¶
- Parameters:
arg__1 –
PySide2.QtCore.QObject
Set the context
object.See also
- PySide2.QtQml.QQmlContext.setContextProperty(arg__1, arg__2)¶
- Parameters:
arg__1 – str
arg__2 –
PySide2.QtCore.QObject
- PySide2.QtQml.QQmlContext.setContextProperty(arg__1, arg__2)
- Parameters:
arg__1 – str
arg__2 – object
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.