| 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
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
statement and exactly one top-level component. In our example,
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.
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).
are the QML equivalents of C++ classes. They have properties, methods (member functions), signals and slots. The occurrence of
in lines 5-9 is an instance of a
component: a blue 300x300 pixel instance of a
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
instance above has three properties
with the values
, 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
would be illegal, because the
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
The order of properties is irrelevant. We could rewrite the
and the result would be the same.
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
component for the photo and a
component for the title on the
component and change the color of the rectangle into the RGB value
// File: BasicSteps_2.qml
import QtQuick 1.0
color: "#FFF8DC" // cornsilk
x: 10; y: 45
x: 10; y: 265
component is a child of the
and is located at x-position 10 and y-position 45 relative to the
'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
property tells the QML runtime where to look for the image, which is stored in the file
. The image file is located in the same directory as the QML file
. In general, the
property takes a URL. The image could be located anywhere in the Internet or our local file system.
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,
is a child of
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
is the root of the tree and
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
x: 10; y: 25
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.
x: 0; y: 235
width: 300; horizontalAlignment: Text.AlignHCenter
height: 65; verticalAlignment: Text.AlignVCenter
It is important to note that the property
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
. So, whenever we use
, we must give
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
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
width: 3''' 300; height: 300
// Photo 1
x: 0; y: 0
// Photo 2
x: 300; y: 0
// Photo 3
x: 2 * 300; y: 0
Photo 1, 2 and 3 are identical copies of the
- except for the properties
With these changes, we see the same photo three times. By changing the properties
, 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
at the beginning of the big rectangle and replace every occurrence of 300 by
. Similarly, we eliminate the magic numbers 10, 25 and 65 by introducing the properties
, 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
. While we are on a roll, we also replace the RGB value
by the property
. After all these modifications, the code for the enclosing rectangle and photo 3 looks as follows (see BasicSteps_4.qml
for the complete code):
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
x: 2 * frameSize; y: 0
width: frameSize; height: frameSize
x: leftMargin; y: topMargin
x: 0; y: frameSize - bottomMargin
text: "Cotton Grass"
width: frameSize; horizontalAlignment: Text.AlignHCenter
height: bottomMargin; verticalAlignment: Text.AlignVCenter
The general syntax for a custom property is
property <type> <name>[: <value>]
keyword is followed by the name of the type,
, and the name of the property,
. The default value,
, for the property is optional. For example, the line
property int frameSize: 300
defines a property with name
, 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
property can be used in the grandchild
of the outermost
, where it is defined. For the moment, we can live with this scoping rule, which we'll amend when needed.
Module 2 - Components
Tutorial Main Page