<?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="index.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="cpp"> <span class="type"><a href="qdeclarativeengine.html">QDeclarativeEngine</a></span> engine; <span class="type"><a href="qstringlistmodel.html">QStringListModel</a></span> modelData; <span class="type">QDeclarativeContext</span> <span class="operator">*</span>context <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QDeclarativeContext</span>(engine<span class="operator">.</span>rootContext()); context<span class="operator">-</span><span class="operator">></span><a href="qdeclarativecontext.html#setContextProperty">setContextProperty</a>(<span class="string">"myModel"</span><span class="operator">,</span> <span class="operator">&</span>modelData); <span class="type"><a href="qdeclarativecomponent.html">QDeclarativeComponent</a></span> component(<span class="operator">&</span>engine); component<span class="operator">.</span>setData(<span class="string">"import QtQuick 1.0\nListView { model: myModel }"</span><span class="operator">,</span> <span class="type"><a href="qurl.html">QUrl</a></span>()); <span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>window <span class="operator">=</span> component<span class="operator">.</span>create(context); </pre> <p>Note it is the responsibility of the creator to delete any QDeclarativeContext it constructs. If the <tt>context</tt> object in the example is no longer needed when the <tt>window</tt> component instance is destroyed, the <tt>context</tt> must be destroyed explicitly. The simplest way to ensure this is to set <tt>window</tt> as the parent of <tt>context</tt>.</p> <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="cpp"> <span class="keyword">class</span> MyDataSet : <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span> { <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span> Q_PROPERTY(<span class="type"><a href="qabstractitemmodel.html">QAbstractItemModel</a></span> <span class="operator">*</span>myModel READ model NOTIFY modelChanged) <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span> }; MyDataSet myDataSet; <span class="type"><a href="qdeclarativeengine.html">QDeclarativeEngine</a></span> engine; <span class="type">QDeclarativeContext</span> <span class="operator">*</span>context <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QDeclarativeContext</span>(engine<span class="operator">.</span>rootContext()); context<span class="operator">-</span><span class="operator">></span><a href="qdeclarativecontext.html#setContextObject">setContextObject</a>(<span class="operator">&</span>myDataSet); <span class="type"><a href="qdeclarativecomponent.html">QDeclarativeComponent</a></span> component(<span class="operator">&</span>engine); component<span class="operator">.</span>setData(<span class="string">"import QtQuick 1.0\nListView { model: myModel }"</span><span class="operator">,</span> <span class="type"><a href="qurl.html">QUrl</a></span>()); component<span class="operator">.</span>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> <a id="the-context-hierarchy" name="the-context-hierarchy" /> <h4>The Context Hierarchy</h4> <p>Contexts form a hierarchy. The root of this hierarchy is the QML engine's <a href="qdeclarativeengine.html#rootContext">root context</a>. 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.</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="cpp"> <span class="type"><a href="qdeclarativeengine.html">QDeclarativeEngine</a></span> engine; <span class="type">QDeclarativeContext</span> <span class="operator">*</span>context1 <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QDeclarativeContext</span>(engine<span class="operator">.</span>rootContext()); <span class="type">QDeclarativeContext</span> <span class="operator">*</span>context2 <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QDeclarativeContext</span>(context1); context1<span class="operator">-</span><span class="operator">></span><a href="qdeclarativecontext.html#setContextProperty">setContextProperty</a>(<span class="string">"a"</span><span class="operator">,</span> <span class="number">12</span>); context1<span class="operator">-</span><span class="operator">></span><a href="qdeclarativecontext.html#setContextProperty">setContextProperty</a>(<span class="string">"b"</span><span class="operator">,</span> <span class="number">12</span>); context2<span class="operator">-</span><span class="operator">></span><a href="qdeclarativecontext.html#setContextProperty">setContextProperty</a>(<span class="string">"b"</span><span class="operator">,</span> <span class="number">15</span>); </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>Warning:</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><b>See also</b> <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><b>See also</b> <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><b>See also</b> <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><b>See also</b> <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><b>See also</b> <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><b>See also</b> <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><b>See also</b> <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.10.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> 2012</td><td align="right" width="25%">Qt 4.8.5</td></tr></table></div></address></body></html>