QDeclarativeContext Class Reference

The QDeclarativeContext class defines a context within a QML engine.

  1. #include <QDeclarativeContext>

Inherits: QObject.

This class was introduced in Qt 4.7.

Detailed Description

The QDeclarativeContext class defines a context within a QML engine.

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

Each QDeclarativeContext 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 QDeclarativeContext::setContextProperty(). The following example shows a Qt model being bound to a context and then accessed from a QML file.

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

Note it is the responsibility of the creator to delete any 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 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.

  1. class MyDataSet : ...  {
  2.     ...
  3.     Q_PROPERTY(QAbstractItemModel *myModel READ model NOTIFY modelChanged)
  4.     ...
  5. };
  6.  
  7. MyDataSet myDataSet;
  8. QDeclarativeEngine engine;
  9. QDeclarativeContext *context = new QDeclarativeContext(engine.rootContext());
  10. context->setContextObject(&myDataSet);
  11.  
  12. QDeclarativeComponent component(&engine);
  13. component.setData("import QtQuick 1.0\nListView  { model: myModel }", QUrl());
  14. 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.

  1.                 QDeclarativeEngine engine;
  2. QDeclarativeContext *context1 = new QDeclarativeContext(engine.rootContext());
  3. QDeclarativeContext *context2 = new QDeclarativeContext(context1);
  4.  
  5. context1->setContextProperty("a", 12);
  6. context1->setContextProperty("b", 12);
  7.  
  8. 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.

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 Using QML Bindings in C++ Applications.

Public Functions

Toggle details QDeclarativeContext

QDeclarativeContextQDeclarativeContext ( QDeclarativeEngine *engine , QObject *parent=0 ...) ( QDeclarativeEngine *engine , QObject *parent=0 )

Create a new QDeclarativeContext as a child of engine's root context, and the QObject parent.

Look up this member in the source code.

Toggle details QDeclarativeContext

QDeclarativeContextQDeclarativeContext ( QDeclarativeContext *parentContext , QObject *parent=0 ...) ( QDeclarativeContext *parentContext , QObject *parent=0 )

Create a new QDeclarativeContext with the given parentContext, and the QObject parent.

Look up this member in the source code.

Toggle details QDeclarativeContext

~QDeclarativeContext~QDeclarativeContext () () [virtual]

Destroys the QDeclarativeContext.

Any expressions, or sub-contexts dependent on this context will be invalidated, but not destroyed (unless they are parented to the QDeclarativeContext object).

Look up this member in the source code.

Toggle details QUrl QDeclarativeContext

baseUrlbaseUrl () ()const

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

See also setBaseUrl().

Look up this member in the source code.

Toggle details QObject * QDeclarativeContext

contextObjectcontextObject () ()const

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

See also setContextObject().

Look up this member in the source code.

Toggle details QVariant QDeclarativeContext

contextPropertycontextProperty ( const QString &name ) ( const QString &name )const

Returns the value of the name property for this context as a QVariant.

See also setContextProperty().

Look up this member in the source code.

Toggle details QDeclarativeEngine * QDeclarativeContext

engineengine () ()const

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

Look up this member in the source code.

Toggle details bool QDeclarativeContext

isValidisValid () ()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.

Look up this member in the source code.

Toggle details QDeclarativeContext * QDeclarativeContext

parentContextparentContext () ()const

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

Look up this member in the source code.

Toggle details QUrl QDeclarativeContext

resolvedUrlresolvedUrl ( const QUrl &src ) ( const QUrl &src )

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

See also QDeclarativeEngine::baseUrl() and setBaseUrl().

Look up this member in the source code.

Toggle details void QDeclarativeContext

setBaseUrlsetBaseUrl ( const QUrl &baseUrl ) ( 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.

See also baseUrl() and resolvedUrl().

Look up this member in the source code.

Toggle details void QDeclarativeContext

setContextObjectsetContextObject ( QObject *object ) ( QObject *object )

Set the context object.

See also contextObject().

Look up this member in the source code.

Toggle details void QDeclarativeContext

setContextPropertysetContextProperty ( const QString &name , QObject *value ...) ( const QString &name , QObject *value )

Set the value of the name property on this context.

QDeclarativeContext does not take ownership of value.

See also contextProperty().

Look up this member in the source code.

Toggle details void QDeclarativeContext

setContextPropertysetContextProperty ( const QString &name , const QVariant &value ...) ( const QString &name , const QVariant &value )

Set a the value of the name property on this context.

Look up this member in the source code.

    Notes provided by the Qt Community

    No notes