QDeclarativeContext

概要

函数

详细描述

PySide.QtDeclarative.QDeclarativeContext class defines a context within a QML engine.

Contexts allow data to be exposed to the QML components instantiated by the QML engine.

PySide.QtDeclarative.QDeclarativeContext contains a set of properties, distinct from its PySide.QtCore.QObject properties, that allow data to be explicitly bound to a context by name. The context properties are defined and updated by calling QDeclarativeContext.setContextProperty() . The following example shows a Qt model being bound to a context and then accessed from a QML file.

QDeclarativeEngine engine;
QStringListModel modelData;
QDeclarativeContext *context = new QDeclarativeContext(engine.rootContext());
context->setContextProperty("myModel", &modelData);
QDeclarativeComponent component(&engine);
component.setData("import QtQuick 1.0\nListView { model: myModel }", QUrl());
QObject *window = component.create(context);
									

Note it is the responsibility of the creator to delete any PySide.QtDeclarative.QDeclarativeContext it constructs. If the context object in the example is no longer needed when the window component instance is destroyed, the context must be destroyed explicitly. The simplest way to ensure this is to set window as the parent of context .

To simplify binding and maintaining larger data sets, a context object can be set on a PySide.QtDeclarative.QDeclarativeContext . All the properties of the context object are available by name in the context, as though they were all individually added through calls to QDeclarativeContext.setContextProperty() . 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 maintaing context property values.

The following example has the same effect as the previous one, but it uses a context object.

class MyDataSet : ... {
    ...
    Q_PROPERTY(QAbstractItemModel *myModel READ model NOTIFY modelChanged)
    ...
};
MyDataSet myDataSet;
QDeclarativeEngine engine;
QDeclarativeContext *context = new QDeclarativeContext(engine.rootContext());
context->setContextObject(&myDataSet);
QDeclarativeComponent component(&engine);
component.setData("import QtQuick 1.0\nListView { model: myModel }", QUrl());
component.create(context);
									

All properties added explicitly by QDeclarativeContext.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 - context1 and context2 . The second context overrides the “b” context property inherited from the first with a new value.

QDeclarativeEngine engine;
QDeclarativeContext *context1 = new QDeclarativeContext(engine.rootContext());
QDeclarativeContext *context2 = new QDeclarativeContext(context1);
context1->setContextProperty("a", 12);
context1->setContextProperty("b", 12);
context2->setContextProperty("b", 15);
										

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.

警告

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.

class PySide.QtDeclarative. QDeclarativeContext ( parent [ , objParent=None ] )
class PySide.QtDeclarative. QDeclarativeContext ( parent [ , objParent=None ] )
参数:
PySide.QtDeclarative.QDeclarativeContext. baseUrl ( )
返回类型: PySide.QtCore.QUrl
PySide.QtDeclarative.QDeclarativeContext. contextObject ( )
返回类型: PySide.QtCore.QObject
PySide.QtDeclarative.QDeclarativeContext. contextProperty ( arg__1 )
参数: arg__1 – unicode
返回类型: object
PySide.QtDeclarative.QDeclarativeContext. engine ( )
返回类型: PySide.QtDeclarative.QDeclarativeEngine
PySide.QtDeclarative.QDeclarativeContext. isValid ( )
返回类型: PySide.QtCore.bool
PySide.QtDeclarative.QDeclarativeContext. parentContext ( )
返回类型: PySide.QtDeclarative.QDeclarativeContext
PySide.QtDeclarative.QDeclarativeContext. resolvedUrl ( arg__1 )
参数: arg__1 PySide.QtCore.QUrl
返回类型: PySide.QtCore.QUrl
PySide.QtDeclarative.QDeclarativeContext. setBaseUrl ( arg__1 )
参数: arg__1 PySide.QtCore.QUrl
PySide.QtDeclarative.QDeclarativeContext. setContextObject ( arg__1 )
参数: arg__1 PySide.QtCore.QObject
PySide.QtDeclarative.QDeclarativeContext. setContextProperty ( arg__1 , arg__2 )
参数:
PySide.QtDeclarative.QDeclarativeContext. setContextProperty ( arg__1 , arg__2 )
参数:
  • arg__1 – unicode
  • arg__2 – object