Qt Quick Tutorial Basics: Difference between revisions
(Updated links) |
(Marked as outdated) |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
{{ | {{Outdated|reason=Qt Quick 1}} | ||
[[Category:Developing_with_Qt::Qt Quick]] | [[Category:Developing_with_Qt::Qt Quick]] | ||
[[Category:Developing_with_Qt::Qt Quick::Tutorial]] | [[Category:Developing_with_Qt::Qt Quick::Tutorial]] | ||
= Qt Quick Tutorial: Module 1 - Basics = | = Qt Quick Tutorial: Module 1 - Basics = |
Latest revision as of 01:12, 24 March 2016
IMPORTANT: The content of this page is outdated. Reason: Qt Quick 1 If you have checked or updated this page and found the content to be suitable, please remove this notice. |
Qt Quick Tutorial: Module 1 - Basics
Components and Properties
No, we won't start with a QML program printing "Hello world!". We'll start with something much more exciting: a QML program that draws a blue rectangle of 300x300 pixels.
// File: BasicSteps_1.qml
import QtQuick 1.0
Rectangle {
width: 300
height: 300
color: "blue"
}
We run QML viewer to see the result of our work:
qmlviewer BasicSteps_1.qml &
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. A QML document is a piece of QML code that contains at least one
import
statement and exactly one top-level component. In our example,
Rectangle
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
import Qt 4.7
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 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
Rectangle
in lines 5-9 is an instance of a
Rectangle
component: a blue 300x300 pixel instance of a
Rectangle
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. An instance of a component is characterized by its properties, which are name-value pairs. The
Rectangle
instance above has three properties
width
,
height
and
color
with the values
300
,
300
and
blue
, respectively. Property names always start with a lowercase letter followed by zero or more letters, digits and underscores. Properties are type-safe, however, the line
width: "Oslo"
would be illegal, because the
width
property 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:
width: 300; height: 300
color: "blue"
The order of properties is irrelevant. We could rewrite the
Rectangle
as
Rectangle {
color: "blue"
height: 300
width: 300
}
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
//
. Line 1 of our example contains such a comment. Multi-line comments start with
/*
and end with
*/
.
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 title below the photo and a cornsilk colored frame. We position a QML
Image
component for the photo and a
Text
component for the title on the
Rectangle
component and change the color of the rectangle into the RGB value
#FFF8DC
for cornsilk.
// File: BasicSteps_2.qml
import QtQuick 1.0
Rectangle {
width: 300
height: 300
color: "#FFF8DC" // cornsilk
Image {
x: 10; y: 45
source: "voringsfossen1.jpg"
}
Text {
x: 10; y: 265
text: "Voringsfossen"
}
}
The
Image
component is a child of the
Rectangle
and is located at x-position 10 and y-position 45 relative to the
Rectangle
's origin (10 pixels to the right and 45 pixels down from the top left corner of the rectangle). As the image is 280x210 pixels, this position centers the image horizontally and vertically within the rectangle. The
source
property tells the QML runtime where to look for the image, which is stored in the file
"voringsfossen1.jpg"
. The image file is located in the same directory as the QML file
BasicSteps.qml
. In general, the
source
property takes a URL. The image could be located anywhere in the Internet or our local file system. The
Text
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,
Text
is a child of
Rectangle
. 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
Rectangle
is the root of the tree and
Image
and
Text
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
Image
's property
y
to
25
.
Image {
x: 10; y: 25
source: "voringsfossen1.jpg"
}
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.
Text {
x: 0; y: 235
text: "Voringsfossen"
font.pixelSize: 20
width: 300; horizontalAlignment: Text.AlignHCenter
height: 65; verticalAlignment: Text.AlignVCenter
}
It is important to note that the property
horizontalAlignment
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
verticalAlignment
. So, whenever we use
horizontalAlignment
and
verticalAlignment
, we must give
width
and
height
, respectively.
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
BasicSteps_2.qml
three times and by enclosing these three rectangles by a big rectangle (see BasicSteps_3.qml for the complete source 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
…
}
}
Photo 1, 2 and 3 are identical copies of the
Rectangle
from
BasicSteps_2.qml
- except for the properties
x
and
y
. 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
3 * 300
and
2 * 300
. With these changes, we see the same photo three times. By changing the properties
source
and
text
, 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 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
frameSize
at the beginning of the big rectangle and replace every occurrence of 300 by
frameSize
. Similarly, we eliminate the magic numbers 10, 25 and 65 by introducing the properties
leftMargin
,
topMargin
and
bottomMargin
, respectively, and by replacing every occurrence of these magic numbers by its properties. We also spot that
235 = 300 - 65 = frameSize - bottomMargin
and replace every occurrence of 235 by the expression
frameSize - bottomMargin
. While we are on a roll, we also replace the RGB value
"#FFF8DC"
by the property
frameColor
. After all these modifications, the code for the enclosing rectangle and photo 3 looks as follows (see BasicSteps_4.qml for the complete 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
}
}
The general syntax for a custom property is
property <type> <name>[: <value>]
The
property
keyword is followed by the name of the type,
<type>
, and the name of the property,
<name>
. The default value,
<value>
, for the property is optional. For example, the line
property int frameSize: 300
defines a property with name
frameSize
, which is of integer type and which has the default value 300. So far, we've 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
leftMargin
property can be used in the grandchild
Image
of the outermost
Rectangle
, where it is defined. For the moment, we can live with this scoping rule, which we'll amend when needed.