Introduction to the QML Language

QML is a declarative language designed to describe the user interface of a program: both what it looks like, and how it behaves. In QML, a user interface is specified as a tree of objects with properties.

This introduction is meant for those with little or no programming experience. JavaScript is used as a scripting language in QML, so you may want to learn a bit more about it (see the Javascript Guide) before diving deeper into QML. It's also helpful to have a basic understanding of other web technologies like HTML and CSS, but it's not required.

Basic QML Syntax

QML looks like this:

  1. import QtQuick 1.0
  3. Rectangle  {
  4.     width: 200
  5.     height: 200
  6.     color: "blue"
  8.     Image  {
  9.         source: "pics/logo.png"
  10.         anchors.centerIn: parent
  11.     }
  12. }

Here we create two objects, a Rectangle object and its child Image object. Objects are specified by their type, followed by a pair of braces in between which additional data can be defined for the object, such as its property values and any child objects.

Properties are specified with a property: value syntax. In the above example, we can see the Image object has a property named source, which has been assigned the value "pics/logo.png". The property and its value are separated by a colon.

Properties can be specified one-per-line:

  1.                 Rectangle  {
  2.     width: 100
  3.     height: 100
  4. }

or you can put multiple properties on a single line:

  1.                 Rectangle  { width: 100; height: 100 }

When multiple property/value pairs are specified on a single line, they must be separated by a semicolon.

The import statement imports the QtQuick module, which contains all of the standard QML Elements. Without this import statement, the Rectangle and Image elements would not be available.


Commenting in QML is similar to JavaScript.

  • Single line comments start with // and finish at the end of the line.
  • Multiline comments start with /* and finish with */
  1.                 Text  {
  2.     text: "Hello world!"    //a basic greeting
  3.     /*
  4.         We want this text to stand out from the rest so
  5.         we give it a large size and different font.
  6.      */
  7. "Helvetica"
  8.     font.pointSize: 24
  9. }

Comments are ignored by the engine. They are useful for explaining what you are doing; for referring back to at a later date, or for others reading your QML files.

Comments can also be used to prevent the execution of code, which is sometimes useful for tracking down problems.

  1.                 Text  {
  2.     text: "Hello world!"
  3.     //opacity: 0.5
  4. }

In the above example, the Text object will have normal opacity, since the line opacity: 0.5 has been turned into a comment.

Object Identifiers

Each object can be given a special id value that allows the object to be identified and referred to by other objects.

For example, below we have two Text objects. The first Text object has an id value of "text1". The second Text object can now set its own text property value to be the same as that of the first object, by referring to text1.text:

  1. import QtQuick 1.0
  3. Row  {
  4.     Text  {
  5.         id: text1
  6.         text: "Hello World"
  7.     }
  9.     Text  { text: text1.text }
  10. }

An object can be referred to by its id from anywhere within the component in which it is declared. Therefore, an id value must always be unique within a single component.

The id value is a special value for a QML object and should not be thought of as an ordinary object property; for example, it is not possible to access in the above example. Once an object is created, its id cannot be changed.

Note that an id must begin with a lower-case letter or an underscore, and cannot contain characters other than letters, numbers and underscores.


JavaScript expressions can be used to assign property values. For example:

  1.                 Item  {
  2.     width: 100 * 3
  3.     height: 50 + 22
  4. }

These expressions can include references to other objects and properties, in which case a binding is established: when the value of the expression changes, the property to which the expression is assigned is automatically updated to the new value. For example:

  1.                 Item  {
  2.     width: 300
  3.     height: 300
  5.     Rectangle  {
  6.         width: parent.width - 50
  7.         height: 100
  8.         color: "yellow"
  9.     }
  10. }

Here, the Rectangle object's width property is set relative to the width of its parent. Whenever the parent's width changes, the width of the Rectangle is automatically updated.


Basic Property Types

QML supports properties of many types (see QML Basic Types). The basic types include int, real, bool, string and color.

  1.                 Item  {
  2.     x: 10.5             // a 'real' property
  3.     state: "details"    // a 'string' property
  4.     focus: true         // a 'bool' property
  5.     // ...
  6. }

QML properties are what is known as type-safe. That is, they only allow you to assign a value that matches the property type. For example, the x property of item is a real, and if you try to assign a string to it you will get an error.

  1. Item  {
  2.     x: "hello"  // illegal!
  3. }

Note that with the exception of Attached Properties, properties always begin with a lowercase letter.

Property Change Notifications

When a property changes value, it can send a signal to notify others of this change.

To receive these signals, simply create a signal handler named with an on<Property>Changed syntax. For example, the Rectangle element has width and color properties. Below, we have a Rectangle object that has defined two signal handlers, onWidthChanged and onColorChanged, which will automaticallly be called whenever these properties are modified:

  1.                 Rectangle  {
  2.     width: 100; height: 100
  4.     onWidthChanged: console.log("Width has changed to:", width)
  5.     onColorChanged: console.log("Color has changed to:", color)
  6. }

Signal handlers are explained further below.

List properties

List properties look like this:

  1.                 Item  {
  2.     children: [
  3.         Image  {},
  4.         Text  {}
  5.     ]
  6. }

The list is enclosed in square brackets, with a comma separating the list elements. In cases where you are only assigning a single item to a list, you can omit the square brackets:

  1.                 Image  {
  2.     children: Rectangle  {}
  3. }

Items in the list can be accessed by index. See the list type documentation for more details about list properties and their available operations.

Default Properties

Each object type can specify one of its list or object properties as its default property. If a property has been declared as the default property, the property tag can be omitted.

For example this code:

  1.                 State  {
  2.     changes: [
  3.         PropertyChanges  {},
  4.         PropertyChanges  {}
  5.     ]
  6. }

can be simplified to:

  1.                 State  {
  2.     PropertyChanges  {}
  3.     PropertyChanges  {}
  4. }

because changes is the default property of the State type.

Grouped Properties

In some cases properties form a logical group and use a 'dot' or grouped notation to show this.

Grouped properties can be written like this:

  1.                 Text  {
  2.     font.pixelSize: 12
  3.     font.bold: true
  4. }

or like this:

  1.                 Text  {
  2.     font  { pixelSize: 12; bold: true }
  3. }

In the element documentation grouped properties are shown using the 'dot' notation.

Attached Properties

Some objects attach properties to another object. Attached Properties are of the form where Type is the type of the element that attaches property.

For example, the ListView element attaches the ListView.isCurrentItem property to each delegate it creates:

  1.                 Component  {
  2.     id: myDelegate
  3.     Text  {
  4.         text: "Hello"
  5.         color: ListView.isCurrentItem ? "red" : "blue"
  6.     }
  7. }
  1.                 ListView  {
  2.     delegate: myDelegate
  3. }

Another example of attached properties is the Keys element which attaches properties for handling key presses to any visual Item, for example:

  1.                 Item  {
  2.     focus: true
  3.     Keys.onSelectPressed: console.log("Selected")
  4. }

Signal Handlers

Signal handlers allow JavaScript code to be executed in response to an event. For example, the MouseArea element has an onClicked handler that can be used to respond to a mouse click. Below, we use this handler to print a message whenever the mouse is clicked:

  1.                 Item  {
  2.     width: 100; height: 100
  4.     MouseArea  {
  5.         anchors.fill: parent
  6.         onClicked:  {
  7.             console.log("mouse button clicked")
  8.         }
  9.     }
  10. }

All signal handlers begin with "on".

Some signal handlers include an optional parameter. For example the MouseArea onPressed signal handler has a mouse parameter that contains information about the mouse press. This parameter can be referred to in the JavaScript code, as below:

  1.                 MouseArea  {
  2.     acceptedButtons: Qt.LeftButton | Qt.RightButton
  3.     onPressed:  {
  4.         if (mouse.button == Qt.RightButton)
  5.             console.log("Right mouse button pressed")
  6.     }
  7. }
Notes provided by the Qt Community
  • 5

Votes: 13

Coverage: Qt library 4.7, 4.8

Picture of Lukas Geyer Lukas Geyer

Lab Rat
4 notes

Signal handlers capitalize the first letter, not character.

Be aware that when QML creates handlers for properties the first letter as in QChar::isLetter() is capitalized. This means, even though the first character might not be a letter (for example an underscore) the first letter found is still capitalized.

  1. property bool __testProperty: false
  3. // RIGHT
  4. on__TestPropertyChanged { ... }
  6. // WRONG
  7. on__testPropertyChanged { ... }