Sophie

Sophie

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

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" />
<!-- qquickloader.cpp -->
  <title>Loader QML Type | Qt Quick 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="qtquick-index.html">Qt Quick</a></td><td ><a href="qtquick-qmlmodule.html">QML Types</a></td><td >Loader QML Type</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtquick-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="#properties">Properties</a></li>
<li class="level1"><a href="#signals">Signals</a></li>
<li class="level1"><a href="#methods">Methods</a></li>
<li class="level1"><a href="#details">Detailed Description</a></li>
<li class="level2"><a href="#loader-sizing-behavior">Loader Sizing Behavior</a></li>
<li class="level2"><a href="#receiving-signals-from-loaded-objects">Receiving Signals from Loaded Objects</a></li>
<li class="level2"><a href="#focus-and-key-events">Focus and Key Events</a></li>
<li class="level2"><a href="#using-a-loader-within-a-view-delegate">Using a Loader within a View Delegate</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Loader QML Type</h1>
<span class="subtitle"></span>
<!-- $$$Loader-brief -->
<p>Allows dynamic loading of a subtree from a URL or Component. <a href="#details">More...</a></p>
<!-- @@@Loader -->
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> Import Statement:</td><td class="memItemRight bottomAlign"> import QtQuick 2.12</td></tr><tr><td class="memItemLeft rightAlign topAlign"> Inherits:</td><td class="memItemRight bottomAlign"> <p><a href="qml-qtquick-item.html">Item</a></p>
</td></tr></table></div><ul>
<li><a href="qml-qtquick-loader-members.html">List of all members, including inherited members</a></li>
</ul>
<a name="properties"></a>
<h2 id="properties">Properties</h2>
<ul>
<li class="fn"><b><b><a href="qml-qtquick-loader.html#active-prop">active</a></b></b> : bool</li>
<li class="fn"><b><b><a href="qml-qtquick-loader.html#asynchronous-prop">asynchronous</a></b></b> : bool</li>
<li class="fn"><b><b><a href="qml-qtquick-loader.html#item-prop">item</a></b></b> : object</li>
<li class="fn"><b><b><a href="qml-qtquick-loader.html#progress-prop">progress</a></b></b> : real</li>
<li class="fn"><b><b><a href="qml-qtquick-loader.html#source-prop">source</a></b></b> : url</li>
<li class="fn"><b><b><a href="qml-qtquick-loader.html#sourceComponent-prop">sourceComponent</a></b></b> : Component</li>
<li class="fn"><b><b><a href="qml-qtquick-loader.html#status-prop">status</a></b></b> : enumeration</li>
</ul>
<a name="signals"></a>
<h2 id="signals">Signals</h2>
<ul>
<li class="fn"><b><b><a href="qml-qtquick-loader.html#loaded-signal">loaded</a></b></b>()</li>
</ul>
<a name="methods"></a>
<h2 id="methods">Methods</h2>
<ul>
<li class="fn">object <b><b><a href="qml-qtquick-loader.html#setSource-method">setSource</a></b></b>(url <i>source</i>,  object <i>properties</i>)</li>
</ul>
<!-- $$$Loader-description -->
<a name="details"></a>
<h2 id="details">Detailed Description</h2>
<p>Loader is used to dynamically load QML components.</p>
<p>Loader can load a QML file (using the <a href="qml-qtquick-loader.html#source-prop">source</a> property) or a Component object (using the <a href="qml-qtquick-loader.html#sourceComponent-prop">sourceComponent</a> property). It is useful for delaying the creation of a component until it is required: for example, when a component should be created on demand, or when a component should not be created unnecessarily for performance reasons.</p>
<p>Here is a Loader that loads &quot;Page1.qml&quot; as a component when the <a href="qml-qtquick-mousearea.html">MouseArea</a> is clicked:</p>
<pre class="qml">

  import QtQuick 2.0

  Item {
      width: 200; height: 200

      Loader { id: pageLoader }

      MouseArea {
          anchors.fill: parent
          onClicked: pageLoader.source = "Page1.qml"
      }
  }

</pre>
<p>The loaded object can be accessed using the <a href="qml-qtquick-loader.html#item-prop">item</a> property.</p>
<p>If the <a href="qml-qtquick-loader.html#source-prop">source</a> or <a href="qml-qtquick-loader.html#sourceComponent-prop">sourceComponent</a> changes, any previously instantiated items are destroyed. Setting <a href="qml-qtquick-loader.html#source-prop">source</a> to an empty string or setting <a href="qml-qtquick-loader.html#sourceComponent-prop">sourceComponent</a> to <code>undefined</code> destroys the currently loaded object, freeing resources and leaving the Loader empty.</p>
<a name="loader-sizing-behavior"></a>
<h3 id="loader-sizing-behavior">Loader Sizing Behavior</h3>
<p>If the source component is not an Item type, Loader does not apply any special sizing rules. When used to load visual types, Loader applies the following sizing rules:</p>
<ul>
<li>If an explicit size is not specified for the Loader, the Loader is automatically resized to the size of the loaded item once the component is loaded.</li>
<li>If the size of the Loader is specified explicitly by setting the width, height or by anchoring, the loaded item will be resized to the size of the Loader.</li>
</ul>
<p>In both scenarios the size of the item and the Loader are identical. This ensures that anchoring to the Loader is equivalent to anchoring to the loaded item.</p>
<div class="table"><table class="generic">
 <tr valign="top" class="odd"><td >sizeloader.qml</td><td >sizeitem.qml</td></tr>
<tr valign="top" class="even"><td ><pre class="qml">

  import QtQuick 2.0

  Item {
    width: 200; height: 200

    Loader {
      // Explicitly set the size of the
      // Loader to the parent item's size
      anchors.fill: parent
      sourceComponent: rect
    }

    Component {
      id: rect
      Rectangle {
        width: 50
        height: 50
        color: "red"
        }
    }
  }

</pre>
</td><td ><pre class="qml">

  import QtQuick 2.0

  Item {
    width: 200; height: 200

    Loader {
      // position the Loader in the center
      // of the parent
      anchors.centerIn: parent
      sourceComponent: rect
    }

    Component {
        id: rect
        Rectangle {
            width: 50
            height: 50
            color: "red"
        }
    }
  }

</pre>
</td></tr>
<tr valign="top" class="odd"><td >The red rectangle will be sized to the size of the root item.</td><td >The red rectangle will be 50x50, centered in the root item.</td></tr>
</table></div>
<a name="receiving-signals-from-loaded-objects"></a>
<h3 id="receiving-signals-from-loaded-objects">Receiving Signals from Loaded Objects</h3>
<p>Any signals emitted from the loaded object can be received using the Connections type. For example, the following <code>application.qml</code> loads <code>MyItem.qml</code>, and is able to receive the <code>message</code> signal from the loaded item through a Connections object:</p>
<div class="table"><table class="generic" width="70%">
 <tr valign="top" class="odd"><td >application.qml</td><td >MyItem.qml</td></tr>
<tr valign="top" class="even"><td ><pre class="qml">

  import QtQuick 2.0

  Item {
      width: 100; height: 100

      Loader {
         id: myLoader
         source: "MyItem.qml"
      }

      Connections {
          target: myLoader.item
          onMessage: console.log(msg)
      }
  }

</pre>
</td><td ><pre class="qml">

  import QtQuick 2.0

  Rectangle {
     id: myItem
     signal message(string msg)

     width: 100; height: 100

     MouseArea {
         anchors.fill: parent
         onClicked: myItem.message("clicked!")
     }
  }

</pre>
</td></tr>
</table></div>
<p>Alternatively, since <code>MyItem.qml</code> is loaded within the scope of the Loader, it could also directly call any function defined in the Loader or its parent <a href="qml-qtquick-item.html">Item</a>.</p>
<a name="focus-and-key-events"></a>
<h3 id="focus-and-key-events">Focus and Key Events</h3>
<p>Loader is a focus scope. Its <a href="qml-qtquick-item.html#focus-prop">focus</a> property must be set to <code>true</code> for any of its children to get the <i>active focus</i>. (See <a href="qtquick-input-focus.html">Keyboard Focus in Qt Quick</a> for more details.) Any key events received in the loaded item should likely also be <a href="qml-qtquick-keyevent.html#accepted-prop">accepted</a> so they are not propagated to the Loader.</p>
<p>For example, the following <code>application.qml</code> loads <code>KeyReader.qml</code> when the <a href="qml-qtquick-mousearea.html">MouseArea</a> is clicked. Notice the <a href="qml-qtquick-item.html#focus-prop">focus</a> property is set to <code>true</code> for the Loader as well as the <a href="qml-qtquick-item.html">Item</a> in the dynamically loaded object:</p>
<div class="table"><table class="generic">
 <tr valign="top" class="odd"><td >application.qml</td><td >KeyReader.qml</td></tr>
<tr valign="top" class="even"><td ><pre class="qml">

  import QtQuick 2.0

  Rectangle {
      width: 200; height: 200

      Loader {
          id: loader
          focus: true
      }

      MouseArea {
          anchors.fill: parent
          onClicked: {
              loader.source = "KeyReader.qml"
          }
      }

      Keys.onPressed: {
          console.log("Captured:",
                      event.text);
      }
  }

</pre>
</td><td ><pre class="qml">

  import QtQuick 2.0

  Item {
      Item {
          focus: true
          Keys.onPressed: {
              console.log("KeyReader captured:",
                          event.text);
              event.accepted = true;
          }
      }
  }

</pre>
</td></tr>
</table></div>
<p>Once <code>KeyReader.qml</code> is loaded, it accepts key events and sets <code>event.accepted</code> to <code>true</code> so that the event is not propagated to the parent <a href="qml-qtquick-rectangle.html">Rectangle</a>.</p>
<p>Since <code>QtQuick 2.0</code>, Loader can also load non-visual components.</p>
<a name="using-a-loader-within-a-view-delegate"></a>
<h3 id="using-a-loader-within-a-view-delegate">Using a Loader within a View Delegate</h3>
<p>In some cases you may wish to use a Loader within a view delegate to improve delegate loading performance. This works well in most cases, but there is one important issue to be aware of related to the creation context of a Component.</p>
<p>In the following example, the <code>index</code> context property inserted by the <a href="qml-qtquick-listview.html">ListView</a> into <code>delegateComponent</code>'s context will be inaccessible to Text, as the Loader will use the creation context of <code>myComponent</code> as the parent context when instantiating it, and <code>index</code> does not refer to anything within that context chain.</p>
<pre class="qml">

  Item {
      width: 400
      height: 400

      Component {
          id: myComponent
          Text { text: index }    //fails
      }

      ListView {
          anchors.fill: parent
          model: 5
          delegate: Component {
              id: delegateComponent
              Loader {
                  sourceComponent: myComponent
              }
          }
      }
  }

</pre>
<p>In this situation we can either move the component inline,</p>
<pre class="qml">

          delegate: Component {
              Loader {
                  sourceComponent: Component {
                      Text { text: index }    //okay
                  }
              }
          }

</pre>
<p>into a separate file,</p>
<pre class="qml">

          delegate: Component {
              Loader {
                  source: "MyComponent.qml" //okay
              }
          }

</pre>
<p>or explicitly set the required information as a property of the Loader (this works because the Loader sets itself as the context object for the component it is loading).</p>
<pre class="qml">

  Item {
      width: 400
      height: 400

      Component {
          id: myComponent
          Text { text: modelIndex }    //okay
      }

      ListView {
          anchors.fill: parent
          model: 5
          delegate: Component {
              Loader {
                  property int modelIndex: index
                  sourceComponent: myComponent
              }
          }
      }
  }

</pre>
<p><b>See also </b>Dynamic Object Creation.</p>
<!-- @@@Loader -->
<h2>Property Documentation</h2>
<!-- $$$active -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="active-prop">
<td class="tblQmlPropNode"><p>
<a name="active-prop"></a><span class="name">active</span> : <span class="type">bool</span></p></td></tr>
</table></div>
</div><div class="qmldoc"><p>This property is <code>true</code> if the Loader is currently active. The default value for this property is <code>true</code>.</p>
<p>If the Loader is inactive, changing the <a href="qml-qtquick-loader.html#source-prop">source</a> or <a href="qml-qtquick-loader.html#sourceComponent-prop">sourceComponent</a> will not cause the item to be instantiated until the Loader is made active.</p>
<p>Setting the value to inactive will cause any <a href="qml-qtquick-loader.html#item-prop">item</a> loaded by the loader to be released, but will not affect the <a href="qml-qtquick-loader.html#source-prop">source</a> or <a href="qml-qtquick-loader.html#sourceComponent-prop">sourceComponent</a>.</p>
<p>The <a href="qml-qtquick-loader.html#status-prop">status</a> of an inactive loader is always <code>Null</code>.</p>
<p><b>See also </b><a href="qml-qtquick-loader.html#source-prop">source</a> and <a href="qml-qtquick-loader.html#sourceComponent-prop">sourceComponent</a>.</p>
</div></div><!-- @@@active -->
<br/>
<!-- $$$asynchronous -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="asynchronous-prop">
<td class="tblQmlPropNode"><p>
<a name="asynchronous-prop"></a><span class="name">asynchronous</span> : <span class="type">bool</span></p></td></tr>
</table></div>
</div><div class="qmldoc"><p>This property holds whether the component will be instantiated asynchronously. By default it is <code>false</code>.</p>
<p>When used in conjunction with the <a href="qml-qtquick-loader.html#source-prop">source</a> property, loading and compilation will also be performed in a background thread.</p>
<p>Loading asynchronously creates the objects declared by the component across multiple frames, and reduces the likelihood of glitches in animation. When loading asynchronously the status will change to Loader.Loading. Once the entire component has been created, the <a href="qml-qtquick-loader.html#item-prop">item</a> will be available and the status will change to Loader.Ready.</p>
<p>Changing the value of this property to <code>false</code> while an asynchronous load is in progress will force immediate, synchronous completion. This allows beginning an asynchronous load and then forcing completion if the Loader content must be accessed before the asynchronous load has completed.</p>
<p>To avoid seeing the items loading progressively set <code>visible</code> appropriately, e.g&#x2e;</p>
<pre class="cpp">

  Loader {
      source: <span class="string">&quot;mycomponent.qml&quot;</span>
      asynchronous: <span class="keyword">true</span>
      visible: status <span class="operator">=</span><span class="operator">=</span> Loader<span class="operator">.</span>Ready
  }

</pre>
<p>Note that this property affects object instantiation only; it is unrelated to loading a component asynchronously via a network.</p>
</div></div><!-- @@@asynchronous -->
<br/>
<!-- $$$item -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="item-prop">
<td class="tblQmlPropNode"><p>
<a name="item-prop"></a><span class="name">item</span> : <span class="type">object</span></p></td></tr>
</table></div>
</div><div class="qmldoc"><p>This property holds the top-level object that is currently loaded.</p>
<p>Since <code>QtQuick 2.0</code>, Loader can load any object type.</p>
</div></div><!-- @@@item -->
<br/>
<!-- $$$progress -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="progress-prop">
<td class="tblQmlPropNode"><p>
<a name="progress-prop"></a><span class="name">progress</span> : <span class="type">real</span></p></td></tr>
</table></div>
</div><div class="qmldoc"><p>This property holds the progress of loading QML data from the network, from 0.0 (nothing loaded) to 1.0 (finished). Most QML files are quite small, so this value will rapidly change from 0 to 1.</p>
<p><b>See also </b><a href="qml-qtquick-loader.html#status-prop">status</a>.</p>
</div></div><!-- @@@progress -->
<br/>
<!-- $$$source -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="source-prop">
<td class="tblQmlPropNode"><p>
<a name="source-prop"></a><span class="name">source</span> : <span class="type">url</span></p></td></tr>
</table></div>
</div><div class="qmldoc"><p>This property holds the URL of the QML component to instantiate.</p>
<p>Since <code>QtQuick 2.0</code>, Loader is able to load any type of object; it is not restricted to Item types.</p>
<p>To unload the currently loaded object, set this property to an empty string, or set <a href="qml-qtquick-loader.html#sourceComponent-prop">sourceComponent</a> to <code>undefined</code>. Setting <code>source</code> to a new URL will also cause the item created by the previous URL to be unloaded.</p>
<p><b>See also </b><a href="qml-qtquick-loader.html#sourceComponent-prop">sourceComponent</a>, <a href="qml-qtquick-loader.html#status-prop">status</a>, and <a href="qml-qtquick-loader.html#progress-prop">progress</a>.</p>
</div></div><!-- @@@source -->
<br/>
<!-- $$$sourceComponent -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="sourceComponent-prop">
<td class="tblQmlPropNode"><p>
<a name="sourceComponent-prop"></a><span class="name">sourceComponent</span> : <span class="type">Component</span></p></td></tr>
</table></div>
</div><div class="qmldoc"><p>This property holds the Component to instantiate.</p>
<pre class="qml">



</pre>
<p>To unload the currently loaded object, set this property to <code>undefined</code>.</p>
<p>Since <code>QtQuick 2.0</code>, Loader is able to load any type of object; it is not restricted to Item types.</p>
<p><b>See also </b><a href="qml-qtquick-loader.html#source-prop">source</a> and <a href="qml-qtquick-loader.html#progress-prop">progress</a>.</p>
</div></div><!-- @@@sourceComponent -->
<br/>
<!-- $$$status -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="status-prop">
<td class="tblQmlPropNode"><p>
<a name="status-prop"></a><span class="name">status</span> : <span class="type">enumeration</span></p></td></tr>
</table></div>
</div><div class="qmldoc"><p>This property holds the status of QML loading. It can be one of:</p>
<ul>
<li>Loader.Null - the loader is inactive or no QML source has been set</li>
<li>Loader.Ready - the QML source has been loaded</li>
<li>Loader.Loading - the QML source is currently being loaded</li>
<li>Loader.Error - an error occurred while loading the QML source</li>
</ul>
<p>Use this status to provide an update or respond to the status change in some way. For example, you could:</p>
<ul>
<li>Trigger a state change:<pre class="qml">



</pre>
</li>
<li>Implement an <code>onStatusChanged</code> signal handler:<pre class="qml">



</pre>
</li>
<li>Bind to the status value:<pre class="qml">



</pre>
</li>
</ul>
<p>Note that if the source is a local file, the status will initially be Ready (or Error). While there will be no onStatusChanged signal in that case, the onLoaded will still be invoked.</p>
<p><b>See also </b><a href="qml-qtquick-loader.html#progress-prop">progress</a>.</p>
</div></div><!-- @@@status -->
<br/>
<h2>Signal Documentation</h2>
<!-- $$$loaded -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="loaded-signal">
<td class="tblQmlFuncNode"><p>
<a name="loaded-signal"></a><span class="name">loaded</span>()</p></td></tr>
</table></div>
</div><div class="qmldoc"><p>This signal is emitted when the <a href="qml-qtquick-loader.html#status-prop">status</a> becomes <code>Loader.Ready</code>, or on successful initial load.</p>
<p>The corresponding handler is <code>onLoaded</code>.</p>
</div></div><!-- @@@loaded -->
<br/>
<h2>Method Documentation</h2>
<!-- $$$setSource -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="setSource-method">
<td class="tblQmlFuncNode"><p>
<a name="setSource-method"></a><span class="type">object</span> <span class="name">setSource</span>(<span class="type">url</span> <i>source</i>,  <span class="type">object</span> <i>properties</i>)</p></td></tr>
</table></div>
</div><div class="qmldoc"><p>Creates an object instance of the given <i>source</i> component that will have the given <i>properties</i>. The <i>properties</i> argument is optional. The instance will be accessible via the <a href="qml-qtquick-loader.html#item-prop">item</a> property once loading and instantiation is complete.</p>
<p>If the <a href="qml-qtquick-loader.html#active-prop">active</a> property is <code>false</code> at the time when this function is called, the given <i>source</i> component will not be loaded but the <i>source</i> and initial <i>properties</i> will be cached. When the loader is made <a href="qml-qtquick-loader.html#active-prop">active</a>, an instance of the <i>source</i> component will be created with the initial <i>properties</i> set.</p>
<p>Setting the initial property values of an instance of a component in this manner will <b>not</b> trigger any associated <a href="qml-qtquick-behavior.html">Behavior</a>s.</p>
<p>Note that the cached <i>properties</i> will be cleared if the <a href="qml-qtquick-loader.html#source-prop">source</a> or <a href="qml-qtquick-loader.html#sourceComponent-prop">sourceComponent</a> is changed after calling this function but prior to setting the loader <a href="qml-qtquick-loader.html#active-prop">active</a>.</p>
<p>Example:</p>
<div class="table"><table class="generic" width="70%">
 <tr valign="top" class="odd"><td ><pre class="qml">



</pre>
</td><td ><pre class="qml">



</pre>
</td></tr>
</table></div>
<p><b>See also </b><a href="qml-qtquick-loader.html#source-prop">source</a> and <a href="qml-qtquick-loader.html#active-prop">active</a>.</p>
</div></div><!-- @@@setSource -->
<br/>
        </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>