Sophie

Sophie

distrib > Mageia > 7 > i586 > media > core-updates > by-pkgid > 6e2327ca1c896c6d674ae53117299f21 > files > 156

qtdeclarative5-doc-5.12.6-1.mga7.noarch.rpm

<?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.12.6</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td >Qt 5.12</td><td ><a href="qtqml-index.html">Qt QML</a></td><td ><a href="qtqml-module.html">C++ Classes</a></td><td >QQmlContext</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtqml-index.html">Qt 5.12.6 Reference Documentation</a></td>
        </tr></table>
      </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-types">Public Types</a></li>
<li class="level1"><a href="#public-functions">Public Functions</a></li>
<li class="level1"><a href="#static-public-members">Static Public Members</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 &lt;QQmlContext&gt;</span>
</td></tr><tr><td class="memItemLeft rightAlign topAlign"> qmake:</td><td class="memItemRight bottomAlign"> QT += qml</td></tr></table></div><ul>
<li><a href="qqmlcontext-members.html">List of all members, including inherited members</a></li>
</ul>
<a name="public-types"></a>
<h2 id="public-types">Public Types</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> class </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext-propertypair.html">PropertyPair</a></b></td></tr>
</table></div>
<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> = nullptr)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#QQmlContext-1">QQmlContext</a></b>(QQmlContext *<i>parentContext</i>, QObject *<i>parent</i> = nullptr)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#dtor.QQmlContext">~QQmlContext</a></b>() override</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 &amp;<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 &amp;<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 &amp;<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#setContextProperties">setContextProperties</a></b>(const QVector&lt;QQmlContext::PropertyPair&gt; &amp;<i>properties</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 &amp;<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-1">setContextProperty</a></b>(const QString &amp;<i>name</i>, const QVariant &amp;<i>value</i>)</td></tr>
</table></div>
<a name="static-public-members"></a>
<h2 id="static-public-members">Static Public Members</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> const QMetaObject </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#staticMetaObject-var">staticMetaObject</a></b></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">&gt;</span>setContextProperty(<span class="string">&quot;myModel&quot;</span><span class="operator">,</span> <span class="operator">&amp;</span>modelData);

  <span class="type"><a href="qqmlcomponent.html">QQmlComponent</a></span> component(<span class="operator">&amp;</span>engine);
  component<span class="operator">.</span>setData(<span class="string">&quot;import QtQuick 2.0\nListView { model: myModel }&quot;</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 maintaining 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">&gt;</span>setContextObject(<span class="operator">&amp;</span>myDataSet);

  <span class="type"><a href="qqmlcomponent.html">QQmlComponent</a></span> component(<span class="operator">&amp;</span>engine);
  component<span class="operator">.</span>setData(<span class="string">&quot;import QtQuick 2.0\nListView { model: myModel }&quot;</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 id="the-context-hierarchy">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 &quot;b&quot; 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">&gt;</span>setContextProperty(<span class="string">&quot;a&quot;</span><span class="operator">,</span> <span class="number">12</span>);
  context1<span class="operator">-</span><span class="operator">&gt;</span>setContextProperty(<span class="string">&quot;b&quot;</span><span class="operator">,</span> <span class="number">12</span>);

  context2<span class="operator">-</span><span class="operator">&gt;</span>setContextProperty(<span class="string">&quot;b&quot;</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 &quot;setup&quot; 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> = nullptr)</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-1"><a name="QQmlContext-1"></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> = nullptr)</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>[override 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> &amp;<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> &amp;<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> &amp;<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 -->
<!-- $$$setContextProperties[overload1]$$$setContextPropertiesconstQVector<QQmlContext::PropertyPair>& -->
<h3 class="fn" id="setContextProperties"><a name="setContextProperties"></a><span class="type">void</span> QQmlContext::<span class="name">setContextProperties</span>(const <span class="type">QVector</span>&lt;<span class="type"><a href="qqmlcontext-propertypair.html">QQmlContext::PropertyPair</a></span>&gt; &amp;<i>properties</i>)</h3>
<p>Set a batch of <i>properties</i> on this context.</p>
<p>Setting all properties in one batch avoids unnecessary refreshing expressions, and is therefore recommended instead of calling <a href="qqmlcontext.html#setContextProperty">setContextProperty</a>() for each individual property.</p>
<p>This function was introduced in  Qt 5.11.</p>
<p><b>See also </b><a href="qqmlcontext.html#setContextProperty">QQmlContext::setContextProperty</a>().</p>
<!-- @@@setContextProperties -->
<!-- $$$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> &amp;<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-1"><a name="setContextProperty-1"></a><span class="type">void</span> QQmlContext::<span class="name">setContextProperty</span>(const <span class="type">QString</span> &amp;<i>name</i>, const <span class="type">QVariant</span> &amp;<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">&copy;</acronym> 2019 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>