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
|
<?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" />
<!-- qmlreusablecomponents.qdoc -->
<title>Qt 4.8: Importing Reusable Components</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>Importing Reusable Components</li>
</ul>
</div>
</div>
<div class="content mainContent">
<link rel="prev" href="qmlevents.html" />
<link rel="next" href="qdeclarativestates.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="qmlevents.html">Signal and Handler Event System</a>
<a class="nextPage" href="qdeclarativestates.html">States</a>
</p><p/>
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#defining-new-components">Defining New Components</a></li>
<li class="level1"><a href="#loading-a-component">Loading a Component</a></li>
<li class="level2"><a href="#importing-a-component">Importing a Component</a></li>
<li class="level2"><a href="#loading-an-inline-component">Loading an Inline Component</a></li>
<li class="level1"><a href="#component-properties">Component Properties</a></li>
<li class="level1"><a href="#top-level-component">Top-Level Component</a></li>
</ul>
</div>
<h1 class="title">Importing Reusable Components</h1>
<span class="subtitle"></span>
<!-- $$$qmlreusablecomponents.html-description -->
<div class="descr"> <a name="details"></a>
<p>A <i>component</i> is an instantiable QML definition, typically contained in a <tt>.qml</tt> file. For instance, a Button <i>component</i> may be defined in <tt>Button.qml</tt>. The QML runtime may instantiate this Button component to create Button <i>objects</i>. Alternatively, a component may be defined inside a <a href="qml-component.html">Component</a> element.</p>
<p>Moreover, the Button definition may also contain other components. A Button component could use a Text element for its label and other components to implement its functions. Compounding components to form new components (and effectively new interfaces) is the emphasis in QML.</p>
<a name="qml-define-components"></a><a name="defining-new-components"></a>
<h2>Defining New Components</h2>
<p>Any snippet of QML code may become a component, by placing the code in a QML file (extension is <tt>.qml</tt>). A complete Button component that responds to user input may be in a Button.qml file.</p>
<pre class="qml"> <span class="comment">//contents of Button.qml</span>
import QtQuick 1.0
<span class="type"><a href="qml-rectangle.html">Rectangle</a></span> {
<span class="name">id</span>: <span class="name">button</span>
<span class="name">width</span>: <span class="number">145</span>; <span class="name">height</span>: <span class="number">60</span>
<span class="name">color</span>: <span class="string">"blue"</span>
<span class="name">smooth</span>: <span class="number">true</span>; <span class="name">radius</span>: <span class="number">9</span>
property <span class="type">alias</span> <span class="name">text</span>: <span class="name">label</span>.<span class="name">text</span>
<span class="type">border</span> {<span class="name">color</span>: <span class="string">"#B9C5D0"</span>; <span class="name">width</span>: <span class="number">1</span>}
<span class="name">gradient</span>: <span class="name">Gradient</span> {
<span class="type"><a href="qml-gradientstop.html">GradientStop</a></span> {<span class="name">color</span>: <span class="string">"#CFF7FF"</span>; <span class="name">position</span>: <span class="number">0.0</span>}
<span class="type"><a href="qml-gradientstop.html">GradientStop</a></span> {<span class="name">color</span>: <span class="string">"#99C0E5"</span>; <span class="name">position</span>: <span class="number">0.57</span>}
<span class="type"><a href="qml-gradientstop.html">GradientStop</a></span> {<span class="name">color</span>: <span class="string">"#719FCB"</span>; <span class="name">position</span>: <span class="number">0.9</span>}
}
<span class="type"><a href="qml-text.html">Text</a></span> {
<span class="name">id</span>: <span class="name">label</span>
<span class="name">anchors</span>.centerIn: <span class="name">parent</span>
<span class="name">text</span>: <span class="string">"Click Me!"</span>
<span class="name">font</span>.pointSize: <span class="number">12</span>
<span class="name">color</span>: <span class="string">"blue"</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">console</span>.<span class="name">log</span>(<span class="name">text</span> <span class="operator">+</span> <span class="string">" clicked"</span>)
}
}</pre>
<p>Alternatively, a <a href="qml-component.html">Component</a> element may encapsulate a QML object to form a component.</p>
<pre class="qml"> <span class="type"><a href="qml-rectangle.html">Rectangle</a></span> {
<span class="type"><a href="qml-component.html">Component</a></span> {
<span class="name">id</span>: <span class="name">inlinecomponent</span>
<span class="type"><a href="qml-rectangle.html">Rectangle</a></span> {
<span class="name">id</span>: <span class="name">display</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">"blue"</span>
}
}
}</pre>
<a name="qml-loading-components"></a><a name="loading-a-component"></a>
<h2>Loading a Component</h2>
<p>The initialization of inline components is different from loading a component from a <tt>.qml</tt> file.</p>
<a name="importing-a-component"></a>
<h3>Importing a Component</h3>
<p>A component defined in a <tt>.qml</tt> file is directly usable by declaring the name of the component. For example, a button defined in <tt>Button.qml</tt> is created by declaring a <tt>Button</tt>. The button is defined in the <a href="#qml-define-components">Defining New Components</a> section.</p>
<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">175</span>; <span class="name">height</span>: <span class="number">350</span>
<span class="name">color</span>: <span class="string">"lightgrey"</span>
<span class="type"><a href="qml-column.html">Column</a></span> {
<span class="name">anchors</span>.centerIn: <span class="name">parent</span>
<span class="name">spacing</span>: <span class="number">15</span>
<span class="type">Button</span> {}
<span class="type">Button</span> {<span class="name">text</span>: <span class="string">"Me Too!"</span>}
<span class="type">Button</span> {<span class="name">text</span>: <span class="string">"Me Three!"</span>}
}
}</pre>
<p>Note that the component name, <tt>Button</tt>, matches the QML filename, <tt>Button.qml</tt>. Also, the first character is in upper case. Matching the names allow components in the same directory to be in the direct import path of the application.</p>
<p>For flexibility, a <tt>qmldir</tt> file is for dictating which additional components, plugins, or directories should be imported. By using a <tt>qmldir</tt> file, component names do not need to match the filenames. The <tt>qmldir</tt> file should, however, be in an imported path.</p>
<pre class="cpp"> Button <span class="operator">.</span><span class="operator">/</span>Button<span class="operator">.</span>qml
FocusButton <span class="operator">.</span><span class="operator">/</span>focusbutton<span class="operator">.</span>qml</pre>
<a name="loading-an-inline-component"></a>
<h3>Loading an Inline Component</h3>
<p>A consequence of inline components is that initialization may be deferred or delayed. A component may be created during a <a href="qml-mousearea.html">MouseArea</a> event or by using a <a href="qml-loader.html">Loader</a> element. The component can create an object, which is addressable in a similar way as an <a href="propertybinding.html#qml-id-property">id property</a>. Thus, the created object may have its bindings set and read like a normal QML object.</p>
<pre class="qml"> <span class="type"><a href="qml-component.html">Component</a></span> {
<span class="name">id</span>: <span class="name">inlinecomponent</span>
<span class="type"><a href="qml-rectangle.html">Rectangle</a></span> {
<span class="name">id</span>: <span class="name">display</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">"blue"</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">inlinecomponent</span>.<span class="name">createObject</span>(<span class="name">parent</span>)
var <span class="name">second</span> = <span class="name">inlinecomponent</span>.<span class="name">createObject</span>(<span class="name">parent</span>)
var <span class="name">third</span> = <span class="name">inlinecomponent</span>.<span class="name">createObject</span>(<span class="name">parent</span>)
<span class="name">third</span>.<span class="name">x</span> <span class="operator">=</span> <span class="name">second</span>.<span class="name">width</span> <span class="operator">+</span> <span class="number">10</span>
<span class="name">third</span>.<span class="name">color</span> <span class="operator">=</span> <span class="string">"red"</span>
}
}</pre>
<a name="qml-component-properties"></a><a name="component-properties"></a>
<h2>Component Properties</h2>
<p>Initializing a component, either from a .qml file or initializing an inline component, have several properties to facilitate component execution. Specifically, there are <a href="propertybinding.html#attached-properties">attached properties</a> and <a href="propertybinding.html#attached-signalhandlers">attached signal handlers</a> for setting properties during the lifetime of a component.</p>
<p>The <tt>Component.onCompleted</tt> attached signal handler is called when the component completes initialization. It is useful for executing any commands after component initialization. Similarly, the <tt>Component.onDestruction</tt> signal handler executes when the component finishes destruction.</p>
<a name="qml-top-level"></a><a name="top-level-component"></a>
<h2>Top-Level Component</h2>
<p>Choosing the <i>top-level</i> or the <i>root</i> object of components is an important design aspect because the top-level object dictates which properties are accessible outside the component. Some elements are not visual elements and will not have visual properties exposed outside the component. Likewise, some elements add functionality that are not available to visual elements.</p>
<p>Consider the Button component from the <a href="#qml-define-components">Defining New Components</a> section; it's top-level object is a <a href="qml-rectangle.html">Rectangle</a>. When imported, the Button component will possess the Rectangle's properties, methods, signals, and any custom properties.</p>
<pre class="qml"> <span class="type"><a href="qml-rectangle.html">Rectangle</a></span> {
<span class="comment">//...</span>
<span class="name">width</span>: <span class="number">145</span>; <span class="name">height</span>: <span class="number">60</span>
<span class="name">color</span>: <span class="string">"blue"</span>
<span class="name">smooth</span>: <span class="number">true</span>; <span class="name">radius</span>: <span class="number">9</span>
property <span class="type">alias</span> <span class="name">text</span>: <span class="name">label</span>.<span class="name">text</span>
<span class="comment">//...</span>
}</pre>
<p>The Button's <tt>text</tt> alias is accessible from outside the component as well as the Rectangle's visual properties and signals such as <tt>x</tt>, <tt>y</tt>, <tt>anchors</tt>, and <tt>states</tt>.</p>
<p>Alternatively, we may choose a <a href="qdeclarativefocus.html">FocusScope</a> as our top-level object. The <a href="qml-focusscope.html">FocusScope</a> element manage keyboard focus for its children which is beneficial for certain types of interfaces. However, since <tt>FocusScopes</tt> are not visual elements, the visual properties of its child need to be exposed.</p>
<pre class="qml"> <span class="comment">//contents of focusbutton.qml</span>
import QtQuick 1.0
<span class="type"><a href="qml-focusscope.html">FocusScope</a></span> {
<span class="comment">//FocusScope needs to bind to visual properties of the children</span>
property <span class="type">alias</span> <span class="name">color</span>: <span class="name">button</span>.<span class="name">color</span>
<span class="name">x</span>: <span class="name">button</span>.<span class="name">x</span>; <span class="name">y</span>: <span class="name">button</span>.<span class="name">y</span>
<span class="name">width</span>: <span class="name">button</span>.<span class="name">width</span>; <span class="name">height</span>: <span class="name">button</span>.<span class="name">height</span>
<span class="type"><a href="qml-rectangle.html">Rectangle</a></span> {
<span class="name">id</span>: <span class="name">button</span>
<span class="name">width</span>: <span class="number">145</span>; <span class="name">height</span>: <span class="number">60</span>
<span class="name">color</span>: <span class="string">"blue"</span>
<span class="name">smooth</span>: <span class="number">true</span>; <span class="name">radius</span>: <span class="number">9</span>
property <span class="type">alias</span> <span class="name">text</span>: <span class="name">label</span>.<span class="name">text</span>
<span class="type">border</span> {<span class="name">color</span>: <span class="string">"#B9C5D0"</span>; <span class="name">width</span>: <span class="number">1</span>}
<span class="name">gradient</span>: <span class="name">Gradient</span> {
<span class="type"><a href="qml-gradientstop.html">GradientStop</a></span> {<span class="name">color</span>: <span class="string">"#CFF7FF"</span>; <span class="name">position</span>: <span class="number">0.0</span>}
<span class="type"><a href="qml-gradientstop.html">GradientStop</a></span> {<span class="name">color</span>: <span class="string">"#99C0E5"</span>; <span class="name">position</span>: <span class="number">0.57</span>}
<span class="type"><a href="qml-gradientstop.html">GradientStop</a></span> {<span class="name">color</span>: <span class="string">"#719FCB"</span>; <span class="name">position</span>: <span class="number">0.9</span>}
}
<span class="type"><a href="qml-text.html">Text</a></span> {
<span class="name">id</span>: <span class="name">label</span>
<span class="name">anchors</span>.centerIn: <span class="name">parent</span>
<span class="name">text</span>: <span class="string">"Click Me!"</span>
<span class="name">font</span>.pointSize: <span class="number">12</span>
<span class="name">color</span>: <span class="string">"blue"</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">console</span>.<span class="name">log</span>(<span class="name">text</span> <span class="operator">+</span> <span class="string">" clicked"</span>)
}
}
}</pre>
</div>
<!-- @@@qmlreusablecomponents.html -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="qmlevents.html">Signal and Handler Event System</a>
<a class="nextPage" href="qdeclarativestates.html">States</a>
</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>
|