Qt Quick Tutorial Basics 2: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Convert ExpressionEngine links)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
#REDIRECT [[Qt Quick Tutorial Basics]]
 
[[Category:Developing_with_Qt::Qt Quick]]
[[Category:Developing_with_Qt::Qt Quick::Tutorial]]
[toc align_right="yes" depth="3"]
 
= Qt Quick Tutorial: Module 1 - Basics =
 
== Components and Properties ==
 
No, we don't start with a QML program printing "Hello world!". We start with something much more exciting: a QML program that draws a blue rectangle of 300x300 pixels.
 
<code>
// File: BasicSteps_1.qml
 
import QtQuick 1.0
 
Rectangle {
width: 300
height: 300
color: "blue"
}
</code>
 
We run QML viewer to see the result of our work:
 
<code>> qmlviewer BasicSteps_1.qml &amp;amp;<code>
 
Yes, the above QML code, indeed, produces a blue rectangle. A self-contained piece of QML code as the one above is called a '''QML document''' (see also [http://doc.qt.nokia.com/4.7-snapshot/qdeclarativedocuments.html QML Documents]). A QML document is a piece of QML code that contains at least one <code>import</code> statement and exactly one top-level component. In our example, <code>Rectangle</code> is the single top-level component. Generally, a QML document corresponds to a file, but it could also be text stored in a string or a URL pointing to QML document stored on a remote server.
 
The statement <code>import Qt 4.7</code> makes all the QML components of Qt 4.7 available to our QML document. Qt 4.7 comes with built-in components like Rectangle, Text, ListView, WebView, Flipable, Animation and many more (see [http://doc.qt.nokia.com/4.7-snapshot/qdeclarativeelements.html QML Elements] for a complete list).
 
'''Components''' are the QML equivalents of C++ classes. They have properties, methods (member functions), signals and slots. The occurrence of <code>Rectangle</code> in lines 5-9 is an instance of a <code>Rectangle</code> component: a blue 300x300 pixel instance of a <code>Rectangle</code> component. In the Qt documentation, '''element''' is often used as a synonym for component. Components with a visual representation like a Rectangle or Text are called '''items'''. Component names always start with an uppercase letter followed by zero or more letters, digits and underscores.
 
A instance of a component is characterised by its '''properties''', which are name-value pairs. The <code>Rectangle</code> instance above has three properties <code>width</code>, <code>height</code> and <code>color</code> with the values <code>300</code>, <code>300</code> and <code>blue</code>, respectively. Property names always start with a lowercase letter followed by zero or more letters, digits and underscores. Properties are type-safe. Hence, the line
 
</code>width: "Oslo"</code>
 
would be illegal, because the property <code>width</code> expects a number of type real. QML supports the following basic types: bool, color, date, font, int, list, point, real, rect, size, string, time, url.
 
Several properties can be written in one line separated by semicolon:
 
<code>
width: 300; height: 300
color: "blue"
</code>
 
The order of properties is irrelevant. We could rewrite the <code>Rectangle</code> as
 
<code>
// File: BlueRectangle.qml
Rectangle {
height: 300
width: 300
color: "blue"
}
</code>
 
and the result would be the same.
 
'''Comments''' are marked in QML in the same way as in C++. Comments to the end of the line start with <code>//</code>. Line 1 of our example contains such a comment. Multi-line comments start with <code>/*</code> and end with <code>*/</code>.
 
h2. Composition of Components
 
The excitement about our blue rectangle is wearing off by now, isn't it? So, let us turn the blue rectangle into a photo frame with a photo in the center, a tag line below the photo and a oldlace colored background. We position a QML <code>Image</code> component for the photo and a <code>Text</code> component for the title on the <code>Rectangle</code> component and change the color to "oldlace". We can use the SVG color names directly in QML.
 
<code>
// File: PlacingElements.qml
import QtQuick 1.0
 
Rectangle {
width: 180
height: 135
color: "oldlace"
Image {
x: 10; y: 10
width: 160
height: 100
fillMode: Image.PreserveAspectCrop
source: "images/image01.jpg"
}
Text {
x: 10; y: 115
width: 100
text: "temple"
}
}
</code>
 
We have chosen a <code>Rectangle</code> element as the root of our element hierarchy. We place an <code>Image</code> and <code>Text</code> element inside. Children in a QML element tree are always placed relative to its parents. The <code>x</code> and <code>y</code> coordinate refers to the top-left … etc
 
As the image is 280x210 pixels, this position centers the image horizontally and vertically within the rectangle. The <code>source</code> property tells the QML runtime where to look for the image, which is stored in the file <code>"voringsfossen1.jpg"</code>. The image file is located in the same directory as the QML file <code>BasicSteps.qml</code>. In general, the <code>source</code> property takes a URL. The image could be located anywhere in the Internet or our local file system.
 
The <code>Text</code> is positioned at 10 pixels to the right and 265 pixels down from the top left corner of the rectangle. The displayed text is "Voringsfossen", which is the name of Norway's most famous and highest waterfall. Again, <code>Text</code> is a child of <code>Rectangle</code> .
 
This simple example of a photo frame shows how to compose basic components into a more complex components. The components or strictly speaking the component instances are arranged in a tree. In our example, the <code>Rectangle</code> is the root of the tree and <code>Image</code> and <code>Text</code> are the root's children. The children could again have children of their own and so on. These instance trees mirror the instance trees of Qt's graphics items or widgets.
 
The result won't win us a design prize, but it is a good first shot. Let us improve it step by step. As the title shouldn't look crammed into the frame below the photo, we move the photo 20 pixels up. We just change <code>Image</code>'s property <code>y</code> to <code>25</code>.
 
<code>
Image {
x: 10; y: 25
source: "voringsfossen1.jpg"
}
</code>
 
We want the title to be a bit bigger, say 20 pixels (line 4), and want it to be centered both horizontally (line 5) and vertically (line 6) in the area below the photo.
 
<code>
Text {
x: 0; y: 235
text: "Voringsfossen"
font.pixelSize: 20
width: 300; horizontalAlignment: Text.AlignHCenter
height: 65; verticalAlignment: Text.AlignVCenter
}
</code>
 
It is important to note that the property <code>horizontalAlignment</code> only has an effect if a width is specified. Without a width, the QML runtime wouldn't know where to position the text (just try it out). The same is true for the property <code>verticalAlignment</code>. So, whenever we use <code>horizontalAlignment</code> and <code>verticalAlignment</code>, we must give <code>width</code> and <code>height</code>, respectively.
 
h2. Custom Properties
 
Before we create our own properties, we create a real need for them. Our next goal is to show three photos side by side. We achieve this by simply instantiating the photo from <code>BasicSteps_2.qml</code> three times and by enclosing these three rectangles by a big rectangle (see [[BasicSteps_3 | BasicSteps_3.qml]] for the complete source code).
 
<code>
 
// File: BasicSteps_3.qml
import QtQuick 1.0
 
Rectangle {
width: 3''' 300; height: 300
 
// Photo 1
Rectangle {
x: 0; y: 0
}
 
// Photo 2
Rectangle {
x: 300; y: 0
}
 
// Photo 3
Rectangle {
x: 2 * 300; y: 0
}
}
</code>
 
Photo 1, 2 and 3 are identical copies of the <code>Rectangle</code> from <code>BasicSteps_2.qml</code> - except for the properties <code>x</code> and <code>y</code>. The position of the photo rectangles are relative to the enclosing rectangle, which has three times the width of a single photo rectangle and the same height. The photos are placed at the x-positions 0, 300 and 600 and at y-position 0 such that they appear side by side. As we see in lines 5 and 21, we can use arbitrary JavaScript expressions for the property values. In the example, we use the expressions <code>3 * 300</code> and <code>2 * 300</code>.
 
With these changes, we see the same photo three times. By changing the properties <code>source</code> and <code>text</code>, we can easily change the image and the title of the photos.
 
The code isn't well written and smells of heavy code duplication. We'll fix the duplication from the three almost identical photo rectangles in the next module [[Qt_Quick_Tutorial_Components | Components]]. We'll fix the duplication from the magical numbers like 300, 25, 65 or 20 right way - by introducing properties.
 
The worst offender is the magic number 300, which occurs 13 times. So, if we want to change the size of our photo frames to, say, 400, we must perform 13 changes - and will typically forget to change at least one occurrence. The solution is to introduce a property <code>frameSize</code> at the beginning of the big rectangle and replace every occurrence of 300 by <code>frameSize</code>. Similarly, we eliminate the magic numbers 10, 25 and 65 by introducing the properties <code>leftMargin</code>, <code>topMargin</code> and <code>bottomMargin</code>, respectively, and by replacing every occurrence of these magic numbers by its properties. We also spot that <code>235 = 300 - 65 = frameSize - bottomMargin</code> and replace every occurrence of 235 by the expression <code>frameSize - bottomMargin</code>. While we are on a roll, we also replace the RGB value <code>"#FFF8DC"</code> by the property <code>frameColor</code>. After all these modifications, the code for the enclosing rectangle and photo 3 looks as follows (see [[BasicSteps_4 | BasicSteps_4.qml]] for the complete code):
 
<code>
Rectangle {
property int frameSize: 300
property int leftMargin: 10
property int topMargin: 25
property int bottomMargin: 65
property int fontSize: 20
property color frameColor: "#FFF8DC" // cornsilk
 
width: 3 * frameSize; height: frameSize
 
 
// Photo 3
Rectangle {
x: 2 * frameSize; y: 0
width: frameSize; height: frameSize
color: frameColor
 
Image {
x: leftMargin; y: topMargin
source: "cotton_grass.jpg"
}
 
Text {
x: 0; y: frameSize - bottomMargin
text: "Cotton Grass"
font.pixelSize: fontSize
width: frameSize; horizontalAlignment: Text.AlignHCenter
height: bottomMargin; verticalAlignment: Text.AlignVCenter
}
}
</code>
 
The general syntax for a '''custom property''' is
<code>
property <type> <name>[: <value>]
</code>
The keyword <code>property</code> is followed by the name of the type, <code>&amp;#60;type&amp;amp;#62;</code>, and the name of the property, <code>&amp;#60;name&amp;amp;#62;</code>. The default value, <code>&amp;#60;value&amp;amp;#62;</code>, for the property is optional. For example, the line
<code>
property int frameSize: 300
</code>
defines a property with name <code>frameSize</code>, which is of integer type and which has the default value 300.
 
So far, we glossed over the '''scoping rules''' for properties, because they worked as expected. The properties of a parent instance are accessible by the child instances, the grandchild instances and so on all the way down the instance tree. This is why, for example, the property <code>leftMargin</code> can be used in the grandchild <code>Image</code> of the outermost <code>Rectangle</code>, where it is defined. For the moment, we can live with this scoping rule, which we'll amend when needed.
 
[http://developer.qt.nokia.com/wiki/Qt_Quick_Tutorial_Components Module 2 - Components]
 
[http://developer.qt.nokia.com/wiki/Qt_Quick_Tutorial Tutorial Main Page]

Latest revision as of 07:05, 29 August 2017