QQmlContext Class

The QQmlContext class defines a context within a QML engine. 更多...

头: #include <QQmlContext>
qmake: QT += qml
继承: QObject

公共函数

QQmlContext (QQmlEngine * engine , QObject * parent = Q_NULLPTR)
QQmlContext (QQmlContext * parentContext , QObject * parent = Q_NULLPTR)
virtual ~QQmlContext ()
QUrl baseUrl () const
QObject * contextObject () const
QVariant contextProperty (const QString & name ) const
QQmlEngine * engine () const
bool isValid () const
QString nameForObject (QObject * object ) const
QQmlContext * parentContext () const
QUrl resolvedUrl (const QUrl & src )
void setBaseUrl (const QUrl & baseUrl )
void setContextObject (QObject * object )
void setContextProperty (const QString & name , QObject * value )
void setContextProperty (const QString & name , const QVariant & value )

额外继承成员

详细描述

The QQmlContext class defines a context within a QML engine.

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

每个 QQmlContext contains a set of properties, distinct from its QObject properties, that allow data to be explicitly bound to a context by name. The context properties are defined and updated by calling QQmlContext::setContextProperty (). 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\nListView { model: myModel }", QUrl());
QObject *window = component.create(context);
					

Note it is the responsibility of the creator to delete any QQmlContext 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 QQmlContext . All the properties of the context object are available by name in the context, as though they were all individually added through calls to QQmlContext::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 maintaining 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;
QQmlEngine engine;
QQmlContext *context = new QQmlContext(engine.rootContext());
context->setContextObject(&myDataSet);
QQmlComponent component(&engine);
component.setData("import QtQuick 2.0\nListView { model: myModel }", QUrl());
component.create(context);
					

All properties added explicitly by QQmlContext::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 根上下文 . 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.

QQmlEngine engine;
QQmlContext *context1 = new QQmlContext(engine.rootContext());
QQmlContext *context2 = new QQmlContext(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.

另请参阅 将 C++ 类型属性暴露给 QML .

成员函数文档编制

QQmlContext:: QQmlContext ( QQmlEngine * engine , QObject * parent = Q_NULLPTR)

创建新的 QQmlContext 作为子级对于 engine 's root context, and the QObject parent .

QQmlContext:: QQmlContext ( QQmlContext * parentContext , QObject * parent = Q_NULLPTR)

创建新的 QQmlContext 采用给定 parentContext ,和 QObject parent .

[虚拟] QQmlContext:: ~QQmlContext ()

销毁 QQmlContext .

Any expressions, or sub-contexts dependent on this context will be invalidated, but not destroyed (unless they are parented to the QQmlContext 对象)。

QUrl QQmlContext:: baseUrl () const

Returns the base url of the component, or the containing component if none is set.

另请参阅 setBaseUrl ().

QObject *QQmlContext:: contextObject () const

Return the context object, or 0 if there is no context object.

另请参阅 setContextObject ().

QVariant QQmlContext:: contextProperty (const QString & name ) const

返回值为 name property for this context as a QVariant .

另请参阅 setContextProperty ().

QQmlEngine *QQmlContext:: engine () const

Return the context's QQmlEngine , or 0 if the context has no QQmlEngine QQmlEngine was destroyed.

bool QQmlContext:: isValid () const

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.

QString QQmlContext:: nameForObject ( QObject * object ) const

Returns the name of object in this context, or an empty string if object is not named in the context. Objects are named by setContextProperty (), or by ids in the case of QML created contexts.

If the object has multiple names, the first is returned.

QQmlContext *QQmlContext:: parentContext () const

Return the context's parent QQmlContext , or 0 if this context has no parent or if the parent has been destroyed.

QUrl QQmlContext:: resolvedUrl (const QUrl & src )

Resolves the URL src relative to the URL of the containing component.

另请参阅 QQmlEngine::baseUrl () 和 setBaseUrl ().

void QQmlContext:: setBaseUrl (const QUrl & baseUrl )

Explicitly sets the url resolvedUrl () will use for relative references to baseUrl .

Calling this function will override the url of the containing component used by default.

另请参阅 baseUrl () 和 resolvedUrl ().

void QQmlContext:: setContextObject ( QObject * object )

Set the context object .

另请参阅 contextObject ().

void QQmlContext:: setContextProperty (const QString & name , QObject * value )

设置 valuename property on this context.

QQmlContext does not 拥有所有权对于 value .

另请参阅 contextProperty ().

void QQmlContext:: setContextProperty (const QString & name , const QVariant & value )

Set a the valuename property on this context.