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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qdeclarativeloader.cpp -->
<title>Qt 4.8: QML Loader Element</title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="content">
<a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
</div>
<div class="breadcrumb toolblock">
<ul>
<li class="first"><a href="index.html">Home</a></li>
<!-- Breadcrumbs go here -->
<li><a href="qdeclarativeelements.html">QML Elements</a></li>
<li>QML Loader Element</li>
</ul>
</div>
</div>
<div class="content mainContent">
<p class="naviNextPrevious headerNavi">
</p><p/>
<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="#signal-handlers">Signal Handlers</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-items">Receiving signals from loaded items</a></li>
<li class="level2"><a href="#focus-and-key-events">Focus and key events</a></li>
</ul>
</div>
<h1 class="title">QML Loader Element</h1>
<span class="subtitle"></span>
<!-- $$$Loader-brief -->
<p>The Loader item allows dynamically loading an Item-based subtree from a URL or Component. <a href="#details">More...</a></p>
<!-- @@@Loader -->
<p>Inherits <a href="qml-item.html">Item</a></p>
<p>This element was introduced in Qt 4.7.</p>
<ul>
<li><a href="qml-loader-members.html">List of all members, including inherited members</a></li>
</ul>
<a name="properties"></a>
<h2>Properties</h2>
<ul>
<li class="fn"><b><b><a href="qml-loader.html#item-prop">item</a></b></b> : Item</li>
<li class="fn"><b><b><a href="qml-loader.html#progress-prop">progress</a></b></b> : real</li>
<li class="fn"><b><b><a href="qml-loader.html#source-prop">source</a></b></b> : url</li>
<li class="fn"><b><b><a href="qml-loader.html#sourceComponent-prop">sourceComponent</a></b></b> : Component</li>
<li class="fn"><b><b><a href="qml-loader.html#status-prop">status</a></b></b> : enumeration</li>
</ul>
<a name="signal-handlers"></a>
<h2>Signal Handlers</h2>
<ul>
<li class="fn"><b><b><a href="qml-loader.html#onLoaded-signal">onLoaded</a></b></b></li>
</ul>
<!-- $$$Loader-description -->
<a name="details"></a>
<h2>Detailed Description</h2>
<p>Loader is used to dynamically load visual QML components. It can load a QML file (using the <a href="qml-loader.html#source-prop">source</a> property) or a <a href="qml-component.html">Component</a> object (using the <a href="qml-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 "Page1.qml" as a component when the <a href="qml-mousearea.html">MouseArea</a> is clicked:</p>
<pre class="qml"> import QtQuick 1.0
<span class="type"><a href="qml-item.html">Item</a></span> {
<span class="name">width</span>: <span class="number">200</span>; <span class="name">height</span>: <span class="number">200</span>
<span class="type">Loader</span> { <span class="name">id</span>: <span class="name">pageLoader</span> }
<span class="type"><a href="qml-mousearea.html">MouseArea</a></span> {
<span class="name">anchors</span>.fill: <span class="name">parent</span>
<span class="name">onClicked</span>: <span class="name">pageLoader</span>.<span class="name">source</span> <span class="operator">=</span> <span class="string">"Page1.qml"</span>
}
}</pre>
<p>The loaded item can be accessed using the <a href="qml-loader.html#item-prop">item</a> property.</p>
<p>If the <a href="qml-loader.html#source-prop">source</a> or <a href="qml-loader.html#sourceComponent-prop">sourceComponent</a> changes, any previously instantiated items are destroyed. Setting <a href="qml-loader.html#source-prop">source</a> to an empty string or setting <a href="qml-loader.html#sourceComponent-prop">sourceComponent</a> to <tt>undefined</tt> destroys the currently loaded item, freeing resources and leaving the Loader empty.</p>
<a name="loader-sizing-behavior"></a>
<h3>Loader sizing behavior</h3>
<p>Loader is like any other visual item and must be positioned and sized accordingly to become visible.</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>
<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 1.0
<span class="type"><a href="qml-item.html">Item</a></span> {
<span class="name">width</span>: <span class="number">200</span>; <span class="name">height</span>: <span class="number">200</span>
<span class="type">Loader</span> {
<span class="comment">// Explicitly set the size of the Loader to the parent item's size</span>
<span class="name">anchors</span>.fill: <span class="name">parent</span>
<span class="name">sourceComponent</span>: <span class="name">rect</span>
}
<span class="type"><a href="qml-component.html">Component</a></span> {
<span class="name">id</span>: <span class="name">rect</span>
<span class="type"><a href="qml-rectangle.html">Rectangle</a></span> {
<span class="name">width</span>: <span class="number">50</span>
<span class="name">height</span>: <span class="number">50</span>
<span class="name">color</span>: <span class="string">"red"</span>
}
}
}</pre>
</td><td ><pre class="qml"> import QtQuick 1.0
<span class="type"><a href="qml-item.html">Item</a></span> {
<span class="name">width</span>: <span class="number">200</span>; <span class="name">height</span>: <span class="number">200</span>
<span class="type">Loader</span> {
<span class="comment">// position the Loader in the center of the parent</span>
<span class="name">anchors</span>.centerIn: <span class="name">parent</span>
<span class="name">sourceComponent</span>: <span class="name">rect</span>
}
<span class="type"><a href="qml-component.html">Component</a></span> {
<span class="name">id</span>: <span class="name">rect</span>
<span class="type"><a href="qml-rectangle.html">Rectangle</a></span> {
<span class="name">width</span>: <span class="number">50</span>
<span class="name">height</span>: <span class="number">50</span>
<span class="name">color</span>: <span class="string">"red"</span>
}
}
}</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>
<a name="receiving-signals-from-loaded-items"></a>
<h3>Receiving signals from loaded items</h3>
<p>Any signals emitted from the loaded item can be received using the <a href="qml-connections.html">Connections</a> element. For example, the following <tt>application.qml</tt> loads <tt>MyItem.qml</tt>, and is able to receive the <tt>message</tt> signal from the loaded item through a <a href="qml-connections.html">Connections</a> object:</p>
<table class="generic">
<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 1.0
<span class="type"><a href="qml-item.html">Item</a></span> {
<span class="name">width</span>: <span class="number">100</span>; <span class="name">height</span>: <span class="number">100</span>
<span class="type">Loader</span> {
<span class="name">id</span>: <span class="name">myLoader</span>
<span class="name">source</span>: <span class="string">"MyItem.qml"</span>
}
<span class="type"><a href="qml-connections.html">Connections</a></span> {
<span class="name">target</span>: <span class="name">myLoader</span>.<span class="name">item</span>
<span class="name">onMessage</span>: <span class="name">console</span>.<span class="name">log</span>(<span class="name">msg</span>)
}
}</pre>
</td><td ><pre class="qml"> import QtQuick 1.0
<span class="type"><a href="qml-rectangle.html">Rectangle</a></span> {
<span class="name">id</span>: <span class="name">myItem</span>
signal <span class="type">message</span>(string msg)
<span class="name">width</span>: <span class="number">100</span>; <span class="name">height</span>: <span class="number">100</span>
<span class="type"><a href="qml-mousearea.html">MouseArea</a></span> {
<span class="name">anchors</span>.fill: <span class="name">parent</span>
<span class="name">onClicked</span>: <span class="name">myItem</span>.<span class="name">message</span>(<span class="string">"clicked!"</span>)
}
}</pre>
</td></tr>
</table>
<p>Alternatively, since <tt>MyItem.qml</tt> 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-item.html">Item</a>.</p>
<a name="focus-and-key-events"></a>
<h3>Focus and key events</h3>
<p>Loader is a focus scope. Its <a href="qml-item.html#focus-prop">focus</a> property must be set to <tt>true</tt> for any of its children to get the <i>active focus</i>. (See <a href="qdeclarativefocus.html#acquiring-focus-and-focus-scopes">the focus documentation page</a> for more details.) Any key events received in the loaded item should likely also be <a href="qml-keyevent.html#accepted-prop">accepted</a> so they are not propagated to the Loader.</p>
<p>For example, the following <tt>application.qml</tt> loads <tt>KeyReader.qml</tt> when the <a href="qml-mousearea.html">MouseArea</a> is clicked. Notice the <a href="qml-item.html#focus-prop">focus</a> property is set to <tt>true</tt> for the Loader as well as the <a href="qml-item.html">Item</a> in the dynamically loaded object:</p>
<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 1.0
<span class="type"><a href="qml-rectangle.html">Rectangle</a></span> {
<span class="name">width</span>: <span class="number">200</span>; <span class="name">height</span>: <span class="number">200</span>
<span class="type">Loader</span> {
<span class="name">id</span>: <span class="name">loader</span>
<span class="name">focus</span>: <span class="number">true</span>
}
<span class="type"><a href="qml-mousearea.html">MouseArea</a></span> {
<span class="name">anchors</span>.fill: <span class="name">parent</span>
<span class="name">onClicked</span>: <span class="name">loader</span>.<span class="name">source</span> <span class="operator">=</span> <span class="string">"KeyReader.qml"</span>
}
<span class="name">Keys</span>.onPressed: {
<span class="name">console</span>.<span class="name">log</span>(<span class="string">"Captured:"</span>, <span class="name">event</span>.<span class="name">text</span>);
}
}</pre>
</td><td ><pre class="qml"> import QtQuick 1.0
<span class="type"><a href="qml-item.html">Item</a></span> {
<span class="type"><a href="qml-item.html">Item</a></span> {
<span class="name">focus</span>: <span class="number">true</span>
<span class="name">Keys</span>.onPressed: {
<span class="name">console</span>.<span class="name">log</span>(<span class="string">"Loaded item captured:"</span>, <span class="name">event</span>.<span class="name">text</span>);
<span class="name">event</span>.<span class="name">accepted</span> <span class="operator">=</span> <span class="number">true</span>;
}
}
}</pre>
</td></tr>
</table>
<p>Once <tt>KeyReader.qml</tt> is loaded, it accepts key events and sets <tt>event.accepted</tt> to <tt>true</tt> so that the event is not propagated to the parent <a href="qml-rectangle.html">Rectangle</a>.</p>
<p><b>See also </b><a href="qml-qt.html#dynamic-object-creation">Dynamic Object Creation</a>.</p>
<!-- @@@Loader -->
<h2>Property Documentation</h2>
<!-- $$$item -->
<div class="qmlitem"><div class="qmlproto"><table class="qmlname"><tr valign="top" class="odd"><td class="tblQmlPropNode"><p><a name="item-prop"></a><span class="qmlreadonly">read-only</span><span class="name">item</span> : <span class="type"><a href="qml-item.html">Item</a></span></p></td></tr></table></div><div class="qmldoc"><p>This property holds the top-level item that is currently loaded.</p>
</div></div><!-- @@@item -->
<br/>
<!-- $$$progress -->
<div class="qmlitem"><div class="qmlproto"><table class="qmlname"><tr valign="top" class="odd"><td class="tblQmlPropNode"><p><a name="progress-prop"></a><span class="qmlreadonly">read-only</span><span class="name">progress</span> : <span class="type"><a href="qml-real.html">real</a></span></p></td></tr></table></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-loader.html#status-prop">status</a>.</p>
</div></div><!-- @@@progress -->
<br/>
<!-- $$$source -->
<div class="qmlitem"><div class="qmlproto"><table class="qmlname"><tr valign="top" class="odd"><td class="tblQmlPropNode"><p><a name="source-prop"></a><span class="name">source</span> : <span class="type"><a href="qml-url.html">url</a></span></p></td></tr></table></div><div class="qmldoc"><p>This property holds the URL of the QML component to instantiate.</p>
<p>Note the QML component must be an <a href="qml-item.html">Item</a>-based component. The loader cannot load non-visual components.</p>
<p>To unload the currently loaded item, set this property to an empty string, or set <a href="qml-loader.html#sourceComponent-prop">sourceComponent</a> to <tt>undefined</tt>. Setting <tt>source</tt> 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-loader.html#sourceComponent-prop">sourceComponent</a>, <a href="qml-loader.html#status-prop">status</a>, and <a href="qml-loader.html#progress-prop">progress</a>.</p>
</div></div><!-- @@@source -->
<br/>
<!-- $$$sourceComponent -->
<div class="qmlitem"><div class="qmlproto"><table class="qmlname"><tr valign="top" class="odd"><td class="tblQmlPropNode"><p><a name="sourceComponent-prop"></a><span class="name">sourceComponent</span> : <span class="type"><a href="qml-component.html">Component</a></span></p></td></tr></table></div><div class="qmldoc"><p>This property holds the <a href="qml-component.html">Component</a> to instantiate.</p>
<pre class="qml"> <span class="type"><a href="qml-item.html">Item</a></span> {
<span class="type"><a href="qml-component.html">Component</a></span> {
<span class="name">id</span>: <span class="name">redSquare</span>
<span class="type"><a href="qml-rectangle.html">Rectangle</a></span> { <span class="name">color</span>: <span class="string">"red"</span>; <span class="name">width</span>: <span class="number">10</span>; <span class="name">height</span>: <span class="number">10</span> }
}
<span class="type"><a href="qml-loader.html">Loader</a></span> { <span class="name">sourceComponent</span>: <span class="name">redSquare</span> }
<span class="type"><a href="qml-loader.html">Loader</a></span> { <span class="name">sourceComponent</span>: <span class="name">redSquare</span>; <span class="name">x</span>: <span class="number">10</span> }
}</pre>
<p>To unload the currently loaded item, set this property to an empty string or <tt>undefined</tt>.</p>
<p><b>See also </b><a href="qml-loader.html#source-prop">source</a> and <a href="qml-loader.html#progress-prop">progress</a>.</p>
</div></div><!-- @@@sourceComponent -->
<br/>
<!-- $$$status -->
<div class="qmlitem"><div class="qmlproto"><table class="qmlname"><tr valign="top" class="odd"><td class="tblQmlPropNode"><p><a name="status-prop"></a><span class="qmlreadonly">read-only</span><span class="name">status</span> : <span class="type"><a href="qml-enumeration.html">enumeration</a></span></p></td></tr></table></div><div class="qmldoc"><p>This property holds the status of QML loading. It can be one of:</p>
<ul>
<li>Loader.Null - 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"> <span class="type"><a href="qml-state.html">State</a></span> { <span class="name">name</span>: <span class="string">'loaded'</span>; <span class="name">when</span>: <span class="name">loader</span>.<span class="name">status</span> <span class="operator">==</span> <span class="name">Loader</span>.<span class="name">Ready</span> }</pre>
</li>
<li>Implement an <tt>onStatusChanged</tt> signal handler:<pre class="qml"> <span class="type"><a href="qml-loader.html">Loader</a></span> {
<span class="name">id</span>: <span class="name">loader</span>
<span class="name">onStatusChanged</span>: <span class="keyword">if</span> (<span class="name">loader</span>.<span class="name">status</span> <span class="operator">==</span> <span class="name">Loader</span>.<span class="name">Ready</span>) <span class="name">console</span>.<span class="name">log</span>(<span class="string">'Loaded'</span>)
}</pre>
</li>
<li>Bind to the status value:<pre class="qml"> <span class="type"><a href="qml-text.html">Text</a></span> { <span class="name">text</span>: <span class="name">loader</span>.<span class="name">status</span> <span class="operator">==</span> <span class="name">Loader</span>.<span class="name">Ready</span> ? <span class="string">'Loaded'</span> : <span class="string">'Not loaded'</span> }</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 <a href="qml-loader.html#onLoaded-signal">onLoaded</a> will still be invoked.</p>
<p><b>See also </b><a href="qml-loader.html#progress-prop">progress</a>.</p>
</div></div><!-- @@@status -->
<br/>
<h2>Signal Handler Documentation</h2>
<!-- $$$onLoaded -->
<div class="qmlitem"><div class="qmlproto"><table class="qmlname"><tr valign="top" class="odd"><td class="tblQmlFuncNode"><p><a name="onLoaded-signal"></a>Loader::<span class="name">onLoaded</span> ()</p></td></tr></table></div><div class="qmldoc"><p>This handler is called when the <a href="qml-loader.html#status-prop">status</a> becomes <tt>Loader.Ready</tt>, or on successful initial load.</p>
</div></div><!-- @@@onLoaded -->
<br/>
<p class="naviNextPrevious footerNavi">
</p>
<div class="ft">
<span></span>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2012 Nokia Corporation and/or its
subsidiaries. Documentation contributions included herein are the copyrights of
their respective owners.</p>
<br />
<p>
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.</p>
<p>
Documentation sources may be obtained from <a href="http://www.qt-project.org">
www.qt-project.org</a>.</p>
<br />
<p>
Nokia, Qt and their respective logos are trademarks of Nokia Corporation
in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. <a title="Privacy Policy"
href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
</div>
</body>
</html>
|