Qt Quick Tutorial Components

From Qt Wiki
Revision as of 08:56, 24 February 2015 by Maintenance script (talk | contribs)
Jump to navigation Jump to search



[toc align_right="yes" depth="3"]

Qt Quick Tutorial: Module 2 - Components

Creating a Custom Component

We still have some work left to do from Module 1. If we look at our final QML document BasicSteps_4.qml, we still see too much code duplication. The code for the three photo rectangles is nearly identical. It only differs in the x-position, the image file and the title. In C+, we would introduce a class <code&gt;Photo&lt;/code&gt;, instantiate it three times and initialize it with the different values of the x-position, image file and title. And, this is actually what we do in QML, too - except that a class is called a component in QML.
A component is always defined in a file that has the component's name as its base name and that has the extension .qml. For example, our <code&gt;Photo&lt;/code&gt; component will be defined in the file Photo.qml. A component name always starts with an uppercase letter and is continued by zero or more letters (uppercase or lowercase), digits or underscores.
We perform the following steps to create the <code&gt;Photo&lt;/code&gt; component.
* We copy the QML file BasicSteps_4.qml into the file Components.qml.
* We create a new QML file Photo.qml in the same directory as Components.qml.
* We copy the <code&gt;Rectangle&lt;/code&gt; for photo 1 into Photo.qml.
We are fairly close to the final <code&gt;Photo&lt;/code&gt; component. We only have to introduce properties for the variable parameters of the <code&gt;Photo&lt;/code&gt; component: the x-position, image file and image title. We will assign concrete values to these properties in the instantiations of the <code&gt;Photo&lt;/code&gt; component in Components.qml.
We don't have to do anything for the x-position, because a <code&gt;Rectangle&lt;/code&gt; already has a property <code&gt;x&lt;/code&gt;. We simply remove the code fragment <code&gt;x: 0;</code&gt;. While we are at it, we also remove the code fragment <code&gt;y: 0&lt;/code&gt;, because the default value for the y-position is 0 anyway.
For the image file and title, we introduce two properties <code&gt;imageFile&lt;/code&gt; and <code&gt;imageTitle&lt;/code&gt; at the beginning of the <code&gt;Rectangle&lt;/code&gt;. Finally, we replace the value of <code&gt;source&lt;/code&gt; in <code&gt;Image&lt;/code&gt; by <code&gt;imageFile&lt;/code&gt; and the value of <code&gt;text&lt;/code&gt; in <code&gt;Text&lt;/code&gt; by <code&gt;imageTitle&lt;/code&gt;. With all these changes, the <code&gt;Photo&lt;/code&gt; component looks as follows.


<br />// File: Photo.qml<br />import QtQuick 1.0
<br />Rectangle {<br /> property url imageFile<br /> property string imageTitle
<br /> width: frameSize; height: frameSize<br /> color: frameColor
<br /> Image {<br /> x: leftMargin; y: topMargin<br /> source: imageFile<br /> }
<br /> Text {<br /> x: 0; y: frameSize - bottomMargin<br /> text: imageTitle<br /> font.pixelSize: fontSize<br /> width: frameSize; horizontalAlignment: Text.AlignHCenter<br /> height: bottomMargin; verticalAlignment: Text.AlignVCenter<br /> }<br />}<br />


We can now use our brand new <code&gt;Photo&lt;/code&gt; component in the main QML document Components.qml (formerly BasicSteps_4.qml), which shows three photos side by side. The resulting code looks as follows.


<br />// File: Components.qml<br />import QtQuick 1.0
<br />Rectangle {<br /> property int frameSize: 300<br /> property int leftMargin: 10<br /> property int topMargin: 25<br /> property int bottomMargin: 65<br /> property int fontSize: 20<br /> property color frameColor: &quot;#FFF8DC&amp;quot; // cornsilk
<br /> width: 3 * frameSize; height: frameSize
<br /> Photo {<br /> x: 0<br /> imageFile: &quot;voringsfossen1.jpg&amp;quot;<br /> imageTitle: &quot;Voringsfossen&amp;quot;<br /> }
<br /> Photo {<br /> x: frameSize<br /> imageFile: &quot;bergen.jpg&amp;quot;<br /> imageTitle: &quot;Bergen&amp;quot;<br /> }
<br /> Photo {<br /> x: 2 * frameSize<br /> imageFile: &quot;cotton_grass.jpg&amp;quot;<br /> imageTitle: &quot;Cotton Grass&amp;quot;<br /> }<br />}<br />


In the three instances of the <code&gt;Photo&lt;/code&gt; component, we only assign values to the three variable parameters: x-position, image file and image title. The values of the other properties like <code&gt;frameSize&lt;/code&gt; or <code&gt;leftMargin&lt;/code&gt; are propagated from the root <code&gt;Rectangle&lt;/code&gt; instance to the <code&gt;Photo&lt;/code&gt; instances and to their child instances <code&gt;Image&lt;/code&gt; and <code&gt;Text&lt;/code&gt;.


We can now admire our work by running QML viewer in the directory of Components.qml and Photo.qml:

qmlviewer Components.qml &amp;#38;<code>
<br />How does QML viewer know where to find the &lt;code&amp;gt;Photo&amp;lt;/code&amp;gt; component? By default, QML viewer looks for a QML file with the same base name as the component and with the extension ''.qml'' in the directory, where it is started. In our case, it finds the file ''Photo.qml''. We'll show in the module [[Qt_Quick_Tutorial_Modules | Modules]] how to place components in other directories than the current one.
<br />h2. Property Aliases and Property id
<br />We can still improve the code of the &lt;code&amp;gt;Photo&amp;lt;/code&amp;gt; component a little bit. The URL of the image file and the image title are stored in two places each. The image file is stored in the newly introduced property &lt;code&amp;gt;imageFile&amp;lt;/code&amp;gt; and in the property &lt;code&amp;gt;source&amp;lt;/code&amp;gt; of &lt;code&amp;gt;Image&amp;lt;/code&amp;gt;. Similarly, the image title is stored in &lt;code&amp;gt;imageTitle&amp;lt;/code&amp;gt; and in &lt;code&amp;gt;text&amp;lt;/code&amp;gt; of &lt;code&amp;gt;Text&amp;lt;/code&amp;gt;. This means that memory is allocated twice for the image file and twice for the image title. It is not a big problem in this example. If, however, the duplicated objects are bigger, it becomes a problem. The other problem is that the we must make sure that the two occurrences of image file and of image title are in sync with each other. Again, it is fairly simple for our little program but it becomes trickier for bigger programs.
<br />Fortunately, property aliases come to our rescue. A '''property alias''' says that two properties share the same value and store it at one memory location. The C''+ equivalent would be a reference to an existing variable. The syntax of a property alias is<br />


property alias property1: property2

<br />where &lt;code&amp;gt;property1&amp;lt;/code&amp;gt; is the newly introduced alias and &lt;code&amp;gt;property2&amp;lt;/code&amp;gt; is the already existing property to be aliased.

In our &lt;code&amp;gt;Photo&amp;lt;/code&amp;gt; component, we want to define &lt;code&amp;gt;imageFile&amp;lt;/code&amp;gt; as an alias for the property &lt;code&amp;gt;source&amp;lt;/code&amp;gt; of &lt;code&amp;gt;Image&amp;lt;/code&amp;gt; and &lt;code&amp;gt;imageTitle&amp;lt;/code&amp;gt; as an alias for the property &lt;code&amp;gt;text&amp;lt;/code&amp;gt; of &lt;code&amp;gt;Text&amp;lt;/code&amp;gt;. There is only one little problem: How can we uniquely address the properties &lt;code&amp;gt;source&amp;lt;/code&amp;gt; and &lt;code&amp;gt;text&amp;lt;/code&amp;gt;? If there were several &lt;code&amp;gt;Image&amp;lt;/code&amp;gt; or &lt;code&amp;gt;Text&amp;lt;/code&amp;gt; components in the &lt;code&amp;gt;Photo&amp;lt;/code&amp;gt; component (a fairly usual scenario, by the way), we would have no way distinguish them.

The special QML '''property &lt;code&amp;gt;id&amp;lt;/code&amp;gt;''' solves this problem. The property &lt;code&amp;gt;id&amp;lt;/code&amp;gt; is a unique name or identifier for a component instance. Using the notation &lt;code&amp;gt;instName.propName&amp;lt;/code&amp;gt; we can access the property &lt;code&amp;gt;propertyName&amp;lt;/code&amp;gt; of the instance, whose &lt;code&amp;gt;id&amp;lt;/code&amp;gt; property has the value &lt;code&amp;gt;instName&amp;lt;/code&amp;gt;.

Now, we can finally introduce the two property aliases and use them. In ''Photo.qml'', we replace the two property definitions in lines 5 and 6 by<br />


property alias imageFile: picture.source
property alias imageTitle: title.text

We assign the identifier &lt;code&amp;gt;photo&amp;lt;/code&amp;gt; to the &lt;code&amp;gt;Image&amp;lt;/code&amp;gt; component and remove the line for the &lt;code&amp;gt;source&amp;lt;/code&amp;gt; property. Then, &lt;code&amp;gt;Image&amp;lt;/code&amp;gt; looks as follows:<br />


Image {
id: picture
x: leftMargin; y: topMargin
}

Similarly, we assign the identifier &lt;code&amp;gt;title&amp;lt;/code&amp;gt; to the &lt;code&amp;gt;Text&amp;lt;/code&amp;gt; component and remove the line for the &lt;code&amp;gt;text&amp;lt;/code&amp;gt; property. Then, &lt;code&amp;gt;Text&amp;lt;/code&amp;gt; looks as follows:<br />


Text {
id: title
x: 0; y: frameSize - bottomMargin
font.pixelSize: fontSize
width: frameSize; horizontalAlignment: Text.AlignHCenter
height: bottomMargin; verticalAlignment: Text.AlignVCenter
}

The complete code for the modified <code&gt;Photo&lt;/code&gt; component can be found in Photo.qml.

It is important to note that the value of <code&gt;id&lt;/code&gt; is not enclosed in double quotes. It is not quoted at all because it is an identifier, not a string.

Go back to "Module 1 - Basics&quot;:http://developer.qt.nokia.com/wiki/Qt_Quick_Tutorial_Basics

"Tutorial Main Page&quot;:http://developer.qt.nokia.com/wiki/Qt_Quick_Tutorial