/usr/share/qt5/doc/qtqml/qqmlcontext.html is in qtdeclarative5-doc-html 5.5.1-2ubuntu6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qqmlcontext.cpp -->
<title>QQmlContext Class | Qt QML 5.5</title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="main">
<div class="main-rounded">
<div class="navigationbar">
<ul>
<li>Qt 5.5</li>
<li><a href="qtqml-index.html">Qt QML</a></li>
<li><a href="qtqml-module.html">C++ Classes</a></li>
<li>QQmlContext</li>
<li id="buildversion">Qt 5.5.1 Reference Documentation</li>
</ul>
</div>
</div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#public-functions">Public Functions</a></li>
<li class="level1"><a href="#details">Detailed Description</a></li>
<li class="level2"><a href="#the-context-hierarchy">The Context Hierarchy</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">QQmlContext Class</h1>
<!-- $$$QQmlContext-brief -->
<p>The <a href="qqmlcontext.html">QQmlContext</a> class defines a context within a QML engine. <a href="#details">More...</a></p>
<!-- @@@QQmlContext -->
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> Header:</td><td class="memItemRight bottomAlign"> <span class="preprocessor">#include <QQmlContext></span>
</td></tr><tr><td class="memItemLeft rightAlign topAlign"> qmake:</td><td class="memItemRight bottomAlign"> QT += qml</td></tr><tr><td class="memItemLeft rightAlign topAlign"> Inherits:</td><td class="memItemRight bottomAlign"> </td></tr></table></div><ul>
<li><a href="qqmlcontext-members.html">List of all members, including inherited members</a></li>
</ul>
<a name="public-functions"></a>
<h2 id="public-functions">Public Functions</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#QQmlContext">QQmlContext</a></b>(QQmlEngine *<i> engine</i>, QObject *<i> parent</i> = 0)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#QQmlContext-2">QQmlContext</a></b>(QQmlContext *<i> parentContext</i>, QObject *<i> parent</i> = 0)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#dtor.QQmlContext">~QQmlContext</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QUrl </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#baseUrl">baseUrl</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QObject * </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#contextObject">contextObject</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QVariant </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#contextProperty">contextProperty</a></b>(const QString &<i> name</i>) const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QQmlEngine * </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#engine">engine</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#isValid">isValid</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QString </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#nameForObject">nameForObject</a></b>(QObject *<i> object</i>) const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QQmlContext * </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#parentContext">parentContext</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QUrl </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#resolvedUrl">resolvedUrl</a></b>(const QUrl &<i> src</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#setBaseUrl">setBaseUrl</a></b>(const QUrl &<i> baseUrl</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#setContextObject">setContextObject</a></b>(QObject *<i> object</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#setContextProperty">setContextProperty</a></b>(const QString &<i> name</i>, QObject *<i> value</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#setContextProperty-2">setContextProperty</a></b>(const QString &<i> name</i>, const QVariant &<i> value</i>)</td></tr>
</table></div>
<a name="details"></a>
<!-- $$$QQmlContext-description -->
<div class="descr">
<h2 id="details">Detailed Description</h2>
<p>The <a href="qqmlcontext.html">QQmlContext</a> 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 <a href="qqmlcontext.html">QQmlContext</a> 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 <a href="qqmlcontext.html#setContextProperty">QQmlContext::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="qqmlengine.html">QQmlEngine</a></span> engine;
<span class="type">QStringListModel</span> modelData;
<span class="type"><a href="qqmlcontext.html#QQmlContext">QQmlContext</a></span> <span class="operator">*</span>context <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qqmlcontext.html#QQmlContext">QQmlContext</a></span>(engine<span class="operator">.</span>rootContext());
context<span class="operator">-</span><span class="operator">></span><a href="qqmlcontext.html#setContextProperty">setContextProperty</a>(<span class="string">"myModel"</span><span class="operator">,</span> <span class="operator">&</span>modelData);
<span class="type"><a href="qqmlcomponent.html">QQmlComponent</a></span> component(<span class="operator">&</span>engine);
component<span class="operator">.</span>setData(<span class="string">"import QtQuick 2.0\nListView { model: myModel }"</span><span class="operator">,</span> <span class="type">QUrl</span>());
<span class="type">QObject</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 <a href="qqmlcontext.html">QQmlContext</a> it constructs. If the <code>context</code> object in the example is no longer needed when the <code>window</code> component instance is destroyed, the <code>context</code> must be destroyed explicitly. The simplest way to ensure this is to set <code>window</code> as the parent of <code>context</code>.</p>
<p>To simplify binding and maintaining larger data sets, a context object can be set on a <a href="qqmlcontext.html">QQmlContext</a>. 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="qqmlcontext.html#setContextProperty">QQmlContext::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">QAbstractItemModel</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="qqmlengine.html">QQmlEngine</a></span> engine;
<span class="type"><a href="qqmlcontext.html#QQmlContext">QQmlContext</a></span> <span class="operator">*</span>context <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qqmlcontext.html#QQmlContext">QQmlContext</a></span>(engine<span class="operator">.</span>rootContext());
context<span class="operator">-</span><span class="operator">></span><a href="qqmlcontext.html#setContextObject">setContextObject</a>(<span class="operator">&</span>myDataSet);
<span class="type"><a href="qqmlcomponent.html">QQmlComponent</a></span> component(<span class="operator">&</span>engine);
component<span class="operator">.</span>setData(<span class="string">"import QtQuick 2.0\nListView { model: myModel }"</span><span class="operator">,</span> <span class="type">QUrl</span>());
component<span class="operator">.</span>create(context);</pre>
<p>All properties added explicitly by <a href="qqmlcontext.html#setContextProperty">QQmlContext::setContextProperty</a>() take precedence over the context object's properties.</p>
<a name="the-context-hierarchy"></a>
<h4 >The Context Hierarchy</h4>
<p>Contexts form a hierarchy. The root of this hierarchy is the QML engine's <a href="qqmlengine.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 - <code>context1</code> and <code>context2</code>. 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="qqmlengine.html">QQmlEngine</a></span> engine;
<span class="type"><a href="qqmlcontext.html#QQmlContext">QQmlContext</a></span> <span class="operator">*</span>context1 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qqmlcontext.html#QQmlContext">QQmlContext</a></span>(engine<span class="operator">.</span>rootContext());
<span class="type"><a href="qqmlcontext.html#QQmlContext">QQmlContext</a></span> <span class="operator">*</span>context2 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qqmlcontext.html#QQmlContext">QQmlContext</a></span>(context1);
context1<span class="operator">-</span><span class="operator">></span><a href="qqmlcontext.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="qqmlcontext.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="qqmlcontext.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>
</div>
<p><b>See also </b><a href="qtqml-cppintegration-exposecppattributes.html">Exposing Attributes of C++ Types to QML</a>.</p>
<!-- @@@QQmlContext -->
<div class="func">
<h2>Member Function Documentation</h2>
<!-- $$$QQmlContext[overload1]$$$QQmlContextQQmlEngine*QObject* -->
<h3 class="fn" id="QQmlContext"><a name="QQmlContext"></a>QQmlContext::<span class="name">QQmlContext</span>(<span class="type"><a href="qqmlengine.html">QQmlEngine</a></span> *<i> engine</i>, <span class="type">QObject</span> *<i> parent</i> = 0)</h3>
<p>Create a new <a href="qqmlcontext.html">QQmlContext</a> as a child of <i>engine</i>'s root context, and the QObject <i>parent</i>.</p>
<!-- @@@QQmlContext -->
<!-- $$$QQmlContext$$$QQmlContextQQmlContext*QObject* -->
<h3 class="fn" id="QQmlContext-2"><a name="QQmlContext-2"></a>QQmlContext::<span class="name">QQmlContext</span>(<span class="type"><a href="qqmlcontext.html#QQmlContext">QQmlContext</a></span> *<i> parentContext</i>, <span class="type">QObject</span> *<i> parent</i> = 0)</h3>
<p>Create a new <a href="qqmlcontext.html">QQmlContext</a> with the given <i>parentContext</i>, and the QObject <i>parent</i>.</p>
<!-- @@@QQmlContext -->
<!-- $$$~QQmlContext[overload1]$$$~QQmlContext -->
<h3 class="fn" id="dtor.QQmlContext"><a name="dtor.QQmlContext"></a><code>[virtual] </code>QQmlContext::<span class="name">~QQmlContext</span>()</h3>
<p>Destroys the <a href="qqmlcontext.html">QQmlContext</a>.</p>
<p>Any expressions, or sub-contexts dependent on this context will be invalidated, but not destroyed (unless they are parented to the <a href="qqmlcontext.html">QQmlContext</a> object).</p>
<!-- @@@~QQmlContext -->
<!-- $$$baseUrl[overload1]$$$baseUrl -->
<h3 class="fn" id="baseUrl"><a name="baseUrl"></a><span class="type">QUrl</span> QQmlContext::<span class="name">baseUrl</span>() const</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="qqmlcontext.html#setBaseUrl">setBaseUrl</a>().</p>
<!-- @@@baseUrl -->
<!-- $$$contextObject[overload1]$$$contextObject -->
<h3 class="fn" id="contextObject"><a name="contextObject"></a><span class="type">QObject</span> * QQmlContext::<span class="name">contextObject</span>() const</h3>
<p>Return the context object, or 0 if there is no context object.</p>
<p><b>See also </b><a href="qqmlcontext.html#setContextObject">setContextObject</a>().</p>
<!-- @@@contextObject -->
<!-- $$$contextProperty[overload1]$$$contextPropertyconstQString& -->
<h3 class="fn" id="contextProperty"><a name="contextProperty"></a><span class="type">QVariant</span> QQmlContext::<span class="name">contextProperty</span>(const <span class="type">QString</span> &<i> name</i>) const</h3>
<p>Returns the value of the <i>name</i> property for this context as a QVariant.</p>
<p><b>See also </b><a href="qqmlcontext.html#setContextProperty">setContextProperty</a>().</p>
<!-- @@@contextProperty -->
<!-- $$$engine[overload1]$$$engine -->
<h3 class="fn" id="engine"><a name="engine"></a><span class="type"><a href="qqmlengine.html">QQmlEngine</a></span> * QQmlContext::<span class="name">engine</span>() const</h3>
<p>Return the context's <a href="qqmlengine.html">QQmlEngine</a>, or 0 if the context has no <a href="qqmlengine.html">QQmlEngine</a> or the <a href="qqmlengine.html">QQmlEngine</a> was destroyed.</p>
<!-- @@@engine -->
<!-- $$$isValid[overload1]$$$isValid -->
<h3 class="fn" id="isValid"><a name="isValid"></a><span class="type">bool</span> QQmlContext::<span class="name">isValid</span>() const</h3>
<p>Returns whether the context is valid.</p>
<p>To be valid, a context must have a engine, and it's <a href="qqmlcontext.html#contextObject">contextObject</a>(), if any, must not have been deleted.</p>
<!-- @@@isValid -->
<!-- $$$nameForObject[overload1]$$$nameForObjectQObject* -->
<h3 class="fn" id="nameForObject"><a name="nameForObject"></a><span class="type">QString</span> QQmlContext::<span class="name">nameForObject</span>(<span class="type">QObject</span> *<i> object</i>) const</h3>
<p>Returns the name of <i>object</i> in this context, or an empty string if <i>object</i> is not named in the context. Objects are named by <a href="qqmlcontext.html#setContextProperty">setContextProperty</a>(), or by ids in the case of QML created contexts.</p>
<p>If the object has multiple names, the first is returned.</p>
<!-- @@@nameForObject -->
<!-- $$$parentContext[overload1]$$$parentContext -->
<h3 class="fn" id="parentContext"><a name="parentContext"></a><span class="type"><a href="qqmlcontext.html#QQmlContext">QQmlContext</a></span> * QQmlContext::<span class="name">parentContext</span>() const</h3>
<p>Return the context's parent <a href="qqmlcontext.html">QQmlContext</a>, or 0 if this context has no parent or if the parent has been destroyed.</p>
<!-- @@@parentContext -->
<!-- $$$resolvedUrl[overload1]$$$resolvedUrlconstQUrl& -->
<h3 class="fn" id="resolvedUrl"><a name="resolvedUrl"></a><span class="type">QUrl</span> QQmlContext::<span class="name">resolvedUrl</span>(const <span class="type">QUrl</span> &<i> src</i>)</h3>
<p>Resolves the URL <i>src</i> relative to the URL of the containing component.</p>
<p><b>See also </b><a href="qqmlengine.html#baseUrl">QQmlEngine::baseUrl</a>() and <a href="qqmlcontext.html#setBaseUrl">setBaseUrl</a>().</p>
<!-- @@@resolvedUrl -->
<!-- $$$setBaseUrl[overload1]$$$setBaseUrlconstQUrl& -->
<h3 class="fn" id="setBaseUrl"><a name="setBaseUrl"></a><span class="type">void</span> QQmlContext::<span class="name">setBaseUrl</span>(const <span class="type">QUrl</span> &<i> baseUrl</i>)</h3>
<p>Explicitly sets the url <a href="qqmlcontext.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="qqmlcontext.html#baseUrl">baseUrl</a>() and <a href="qqmlcontext.html#resolvedUrl">resolvedUrl</a>().</p>
<!-- @@@setBaseUrl -->
<!-- $$$setContextObject[overload1]$$$setContextObjectQObject* -->
<h3 class="fn" id="setContextObject"><a name="setContextObject"></a><span class="type">void</span> QQmlContext::<span class="name">setContextObject</span>(<span class="type">QObject</span> *<i> object</i>)</h3>
<p>Set the context <i>object</i>.</p>
<p><b>See also </b><a href="qqmlcontext.html#contextObject">contextObject</a>().</p>
<!-- @@@setContextObject -->
<!-- $$$setContextProperty[overload1]$$$setContextPropertyconstQString&QObject* -->
<h3 class="fn" id="setContextProperty"><a name="setContextProperty"></a><span class="type">void</span> QQmlContext::<span class="name">setContextProperty</span>(const <span class="type">QString</span> &<i> name</i>, <span class="type">QObject</span> *<i> value</i>)</h3>
<p>Set the <i>value</i> of the <i>name</i> property on this context.</p>
<p><a href="qqmlcontext.html">QQmlContext</a> does <b>not</b> take ownership of <i>value</i>.</p>
<p><b>See also </b><a href="qqmlcontext.html#contextProperty">contextProperty</a>().</p>
<!-- @@@setContextProperty -->
<!-- $$$setContextProperty$$$setContextPropertyconstQString&constQVariant& -->
<h3 class="fn" id="setContextProperty-2"><a name="setContextProperty-2"></a><span class="type">void</span> QQmlContext::<span class="name">setContextProperty</span>(const <span class="type">QString</span> &<i> name</i>, const <span class="type">QVariant</span> &<i> value</i>)</h3>
<p>Set a the <i>value</i> of the <i>name</i> property on this context.</p>
<!-- @@@setContextProperty -->
</div>
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2015 The Qt Company Ltd.
Documentation contributions included herein are the copyrights of
their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. </p>
</div>
</body>
</html>
|