<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html><head><title>QDeclarativeContext Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm } a:link { color: #004faf; text-decoration: none } a:visited { color: #672967; text-decoration: none } td.postheader { font-family: sans-serif } tr.address { font-family: sans-serif } body { background: #ffffff; color: black; } </style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">  </td><td class="postheader" valign="center"><a href="../pyqt4ref.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QDeclarativeContext Class Reference<br /><sup><sup>[<a href="qtdeclarative.html">QtDeclarative</a> module]</sup></sup></h1><p>The QDeclarativeContext class defines a context within a QML engine. <a href="#details">More...</a></p> <p>Inherits <a href="qobject.html">QObject</a>.</p><h3>Methods</h3><ul><li><div class="fn" /><b><a href="qdeclarativecontext.html#QDeclarativeContext">__init__</a></b> (<i>self</i>, QDeclarativeEngine <i>engine</i>, QObject <i>parent</i> = None)</li><li><div class="fn" /><b><a href="qdeclarativecontext.html#QDeclarativeContext-2">__init__</a></b> (<i>self</i>, QDeclarativeContext <i>context</i>, QObject <i>parent</i> = None)</li><li><div class="fn" />QUrl <b><a href="qdeclarativecontext.html#baseUrl">baseUrl</a></b> (<i>self</i>)</li><li><div class="fn" />QObject <b><a href="qdeclarativecontext.html#contextObject">contextObject</a></b> (<i>self</i>)</li><li><div class="fn" />QVariant <b><a href="qdeclarativecontext.html#contextProperty">contextProperty</a></b> (<i>self</i>, QString)</li><li><div class="fn" />QDeclarativeEngine <b><a href="qdeclarativecontext.html#engine">engine</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qdeclarativecontext.html#isValid">isValid</a></b> (<i>self</i>)</li><li><div class="fn" />QDeclarativeContext <b><a href="qdeclarativecontext.html#parentContext">parentContext</a></b> (<i>self</i>)</li><li><div class="fn" />QUrl <b><a href="qdeclarativecontext.html#resolvedUrl">resolvedUrl</a></b> (<i>self</i>, QUrl)</li><li><div class="fn" /><b><a href="qdeclarativecontext.html#setBaseUrl">setBaseUrl</a></b> (<i>self</i>, QUrl)</li><li><div class="fn" /><b><a href="qdeclarativecontext.html#setContextObject">setContextObject</a></b> (<i>self</i>, QObject)</li><li><div class="fn" /><b><a href="qdeclarativecontext.html#setContextProperty">setContextProperty</a></b> (<i>self</i>, QString, QObject)</li><li><div class="fn" /><b><a href="qdeclarativecontext.html#setContextProperty-2">setContextProperty</a></b> (<i>self</i>, QString, QVariant)</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QDeclarativeContext class defines a context within a QML engine.</p> <p>Contexts allow data to be exposed to the QML components instantiated by the QML engine.</p> <p>Each QDeclarativeContext contains a set of properties, distinct from its <a href="qobject.html">QObject</a> properties, that allow data to be explicitly bound to a context by name. The context properties are defined and updated by calling <a href="qdeclarativecontext.html#setContextProperty">QDeclarativeContext.setContextProperty</a>(). The following example shows a Qt model being bound to a context and then accessed from a QML file.</p> <pre class="highlightedCode brush: cpp"> 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()); component.create(context); </pre> <p>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 <a href="qdeclarativecontext.html#setContextProperty">QDeclarativeContext.setContextProperty</a>(). 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.</p> <p>The following example has the same effect as the previous one, but it uses a context object.</p> <pre class="highlightedCode brush: cpp"> 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); </pre> <p>All properties added explicitly by <a href="qdeclarativecontext.html#setContextProperty">QDeclarativeContext.setContextProperty</a>() take precedence over the context object's properties.</p> <p>Contexts form a hierarchy. The root of this hierarchy is the <a href="qdeclarativeengine.html">QDeclarativeEngine</a>'s <a href="qdeclarativeengine.html#rootContext">root context</a>. 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 <a href="qdeclarativeengine.html#rootContext">root context</a>.</p> <p>The following example defines two contexts - <tt>context1</tt> and <tt>context2</tt>. The second context overrides the "b" context property inherited from the first with a new value.</p> <pre class="highlightedCode brush: cpp"> 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); </pre> <p>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.</p> <p><b>Note:</b> 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.</p> <hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QDeclarativeContext" />QDeclarativeContext.__init__ (<i>self</i>, <a href="qdeclarativeengine.html">QDeclarativeEngine</a> <i>engine</i>, <a href="qobject.html">QObject</a> <i>parent</i> = None)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Create a new <a href="qdeclarativecontext.html">QDeclarativeContext</a> as a child of <i>engine</i>'s root context, and the <a href="qobject.html">QObject</a> <i>parent</i>.</p> <h3 class="fn"><a name="QDeclarativeContext-2" />QDeclarativeContext.__init__ (<i>self</i>, <a href="qdeclarativecontext.html">QDeclarativeContext</a> <i>context</i>, <a href="qobject.html">QObject</a> <i>parent</i> = None)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Create a new <a href="qdeclarativecontext.html">QDeclarativeContext</a> with the given <i>parentContext</i>, and the <a href="qobject.html">QObject</a> <i>parent</i>.</p> <h3 class="fn"><a name="baseUrl" /><a href="qurl.html">QUrl</a> QDeclarativeContext.baseUrl (<i>self</i>)</h3><p>Returns the base url of the component, or the containing component if none is set.</p> <p>See also <a href="qdeclarativecontext.html#setBaseUrl">setBaseUrl</a>().</p> <h3 class="fn"><a name="contextObject" /><a href="qobject.html">QObject</a> QDeclarativeContext.contextObject (<i>self</i>)</h3><p>Return the context object, or 0 if there is no context object.</p> <p>See also <a href="qdeclarativecontext.html#setContextObject">setContextObject</a>().</p> <h3 class="fn"><a name="contextProperty" />QVariant QDeclarativeContext.contextProperty (<i>self</i>, QString)</h3><p>Returns the value of the <i>name</i> property for this context as a <a href="qvariant.html">QVariant</a>.</p> <p>See also <a href="qdeclarativecontext.html#setContextProperty">setContextProperty</a>().</p> <h3 class="fn"><a name="engine" /><a href="qdeclarativeengine.html">QDeclarativeEngine</a> QDeclarativeContext.engine (<i>self</i>)</h3><p>Return the context's <a href="qdeclarativeengine.html">QDeclarativeEngine</a>, or 0 if the context has no <a href="qdeclarativeengine.html">QDeclarativeEngine</a> or the <a href="qdeclarativeengine.html">QDeclarativeEngine</a> was destroyed.</p> <h3 class="fn"><a name="isValid" />bool QDeclarativeContext.isValid (<i>self</i>)</h3><p>Returns whether the context is valid.</p> <p>To be valid, a context must have a engine, and it's <a href="qdeclarativecontext.html#contextObject">contextObject</a>(), if any, must not have been deleted.</p> <h3 class="fn"><a name="parentContext" /><a href="qdeclarativecontext.html">QDeclarativeContext</a> QDeclarativeContext.parentContext (<i>self</i>)</h3><p>Return the context's parent <a href="qdeclarativecontext.html">QDeclarativeContext</a>, or 0 if this context has no parent or if the parent has been destroyed.</p> <h3 class="fn"><a name="resolvedUrl" /><a href="qurl.html">QUrl</a> QDeclarativeContext.resolvedUrl (<i>self</i>, <a href="qurl.html">QUrl</a>)</h3><p>Resolves the URL <i>src</i> relative to the URL of the containing component.</p> <p>See also <a href="qdeclarativeengine.html#baseUrl">QDeclarativeEngine.baseUrl</a>() and <a href="qdeclarativecontext.html#setBaseUrl">setBaseUrl</a>().</p> <h3 class="fn"><a name="setBaseUrl" />QDeclarativeContext.setBaseUrl (<i>self</i>, <a href="qurl.html">QUrl</a>)</h3><p>Explicitly sets the url <a href="qdeclarativecontext.html#resolvedUrl">resolvedUrl</a>() will use for relative references to <i>baseUrl</i>.</p> <p>Calling this function will override the url of the containing component used by default.</p> <p>See also <a href="qdeclarativecontext.html#baseUrl">baseUrl</a>() and <a href="qdeclarativecontext.html#resolvedUrl">resolvedUrl</a>().</p> <h3 class="fn"><a name="setContextObject" />QDeclarativeContext.setContextObject (<i>self</i>, <a href="qobject.html">QObject</a>)</h3><p>Set the context <i>object</i>.</p> <p>See also <a href="qdeclarativecontext.html#contextObject">contextObject</a>().</p> <h3 class="fn"><a name="setContextProperty" />QDeclarativeContext.setContextProperty (<i>self</i>, QString, <a href="qobject.html">QObject</a>)</h3><p>Set the <i>value</i> of the <i>name</i> property on this context.</p> <p><a href="qdeclarativecontext.html">QDeclarativeContext</a> does <b>not</b> take ownership of <i>value</i>.</p> <p>See also <a href="qdeclarativecontext.html#contextProperty">contextProperty</a>().</p> <h3 class="fn"><a name="setContextProperty-2" />QDeclarativeContext.setContextProperty (<i>self</i>, QString, QVariant)</h3><p>Set a the <i>value</i> of the <i>name</i> property on this context.</p> <address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt 4.8.3 for X11</td><td align="center" width="50%">Copyright © <a href="http://www.riverbankcomputing.com">Riverbank Computing Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2011</td><td align="right" width="25%">Qt 4.7.1</td></tr></table></div></address></body></html>