The QDeclarativeContext class defines a context within a QML engine. More...
#include <QDeclarativeContext>
Inherits QObject.
This class was introduced in Qt 4.7.
QDeclarativeContext ( QDeclarativeEngine * engine, QObject * parent = 0 ) | |
QDeclarativeContext ( QDeclarativeContext * parentContext, QObject * parent = 0 ) | |
virtual | ~QDeclarativeContext () |
QUrl | baseUrl () const |
QObject * | contextObject () const |
QVariant | contextProperty ( const QString & name ) const |
QDeclarativeEngine * | engine () const |
bool | isValid () const |
QDeclarativeContext * | 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 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.
QDeclarativeEngine engine; QStringListModel modelData; QDeclarativeContext *context = new QDeclarativeContext(engine.rootContext()); context->setContextProperty("myModel", &modelData); QDeclarativeComponent component(&engine); component.setData("import Qt 4.7\nListView { model: myModel }", QUrl()); component.create(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.
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 Qt 4.7\nListView { model: myModel }", QUrl()); component.create(context);
All properties added explicitly by QDeclarativeContext::setContextProperty() take precedence over the context object's properties.
Contexts form a hierarchy. The root of this hierarchy is the QDeclarativeEngine's root context. A component instance can access the data in its own context, as well as all its ancestor contexts. Data can be made available to all instances by modifying the root context.
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.
Note: 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 in C++ Applications.
Create a new QDeclarativeContext as a child of engine's root context, and the QObject parent.
Create a new QDeclarativeContext with the given parentContext, and the QObject parent.
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).
Returns the base url of the component, or the containing component if none is set.
See also setBaseUrl().
Return the context object, or 0 if there is no context object.
See also setContextObject().
Returns the value of the name property for this context as a QVariant.
See also setContextProperty().
Return the context's QDeclarativeEngine, or 0 if the context has no QDeclarativeEngine or the QDeclarativeEngine was destroyed.
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.
Return the context's parent QDeclarativeContext, or 0 if this context has no parent or if the parent has been destroyed.
Resolves the URL src relative to the URL of the containing component.
See also QDeclarativeEngine::baseUrl() and setBaseUrl().
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().
Set the context object.
See also contextObject().
Set the value of the name property on this context.
QDeclarativeContext does not take ownership of value.
See also contextProperty().
Set a the value of the name property on this context.