QML 句法基础

QML is a multi-paradigm language that enables objects to be defined in terms of their attributes and how they relate and respond to changes in other objects. In contrast to purely imperative code, where changes in attributes and behavior are expressed through a series of statements that are processed step by step, QML's declarative syntax integrates attribute and behavioral changes directly into the definitions of individual objects. These attribute definitions can then include imperative code, in the case where complex custom application behavior is needed.

QML source code is generally loaded by the engine through QML documents , which are standalone documents of QML code. These can be used to define QML 对象类型 that can then be reused throughout an application. Note that type names must begin with an uppercase letter in order to be declared as QML object types in a QML file.

import 语句

A QML document may have one or more imports at the top of the file. An import can be any one of:

  • a versioned namespace into which types have been registered (e.g., by a plugin)
  • a relative directory which contains type-definitions as QML documents
  • JavaScript 文件

JavaScript file imports must be qualified when imported, so that the properties and methods they provide can be accessed.

各种 import 的一般形式如下:

  • import Namespace VersionMajor.VersionMinor
  • import Namespace VersionMajor.VersionMinor as SingletonTypeIdentifier
  • import "directory"
  • import "file.js" as ScriptIdentifier

范例:

  • import QtQuick 2.0
  • import QtQuick.LocalStorage 2.0 as Database
  • import "../privateComponents"
  • import "somefile.js" as Script

请参阅 QML 句法 - import 语句 文档编制,了解 QML import 的有关深入信息。

对象声明

Syntactically, a block of QML code defines a tree of QML objects to be created. Objects are defined using 对象声明 that describe the type of object to be created as well as the attributes that are to be given to the object. Each object may also declare child objects using nested object declarations.

An object declaration consists of the name of its object type, followed by a set of curly braces. All attributes and child objects are then declared within these braces.

这里是简单对象声明:

Rectangle {
    width: 100
    height: 100
    color: "red"
}
					

This declares an object of type Rectangle , followed by a set of curly braces that encompasses the attributes defined for that object. The Rectangle type is a type made available by the QtQuick module, and the attributes defined in this case are the values of the rectangle's width , height and color properties. (These are properties made available by the Rectangle type, as described in the Rectangle 文档编制)。

The above object can be loaded by the engine if it is part of a QML 文档 . That is, if the source code is complemented with import statement that imports the QtQuick module (to make the Rectangle type available), as below:

import QtQuick 2.0
Rectangle {
    width: 100
    height: 100
    color: "red"
}
					

When placed into a .qml file and loaded by the QML engine, the above code creates a Rectangle 对象使用 Rectangle type supplied by the QtQuick 模块:

注意: If an object definition only has a small number of properties, it can be written on a single line like this, with the properties separated by semi-colons:

Rectangle { width: 100; height: 100; color: "red" }
					

Obviously, the Rectangle object declared in this example is very simple indeed, as it defines nothing more than a few property values. To create more useful objects, an object declaration may define many other types of attributes: these are discussed in the QML 对象属性 documentation. Additionally, an object declaration may define child objects, as discussed below.

子级对象

Any object declaration can define child objects through nested object declarations. In this way, any object declaration implicitly declares an object tree that may contain any number of child objects .

例如, Rectangle object declaration below includes a Gradient object declaration, which in turn contains two GradientStop 声明:

import QtQuick 2.0
Rectangle {
    width: 100
    height: 100
    gradient: Gradient {
        GradientStop { position: 0.0; color: "yellow" }
        GradientStop { position: 1.0; color: "green" }
    }
}
					

When this code is loaded by the engine, it creates an object tree with a Rectangle object at the root; this object has a Gradient child object, which in turn has two GradientStop children.

Note, however, that this is a parent-child relationship in the context of the QML object tree, not in the context of the visual scene. The concept of a parent-child relationship in a visual scene is provided by the Item 类型从 QtQuick module, which is the base type for most QML types, as most QML objects are intended to be visually rendered. For example, Rectangle and Text are both Item -based types, and below, a Text object has been declared as a visual child of a Rectangle 对象:

import QtQuick 2.0
Rectangle {
    width: 200
    height: 200
    color: "red"
    Text {
        anchors.centerIn: parent
        text: "Hello, QML!"
    }
}
					

Text object refers to its parent value in the above code, it is referring to its visual parent , not the parent in the object tree. In this case, they are one and the same: the Rectangle object is the parent of the Text object in both the context of the QML object tree as well as the context of the visual scene. However, while the parent property can be modified to change the visual parent, the parent of an object in the context of the object tree cannot be changed from QML.

(Additionally, notice that the Text object has been declared without assigning it to a property of the Rectangle , unlike the earlier example which assigned a Gradient object to the rectangle's gradient property. This is because the children property of Item has been set as the type's default property to enable this more convenient syntax.)

visual parent documentation for more information on the concept of visual parenting with the Item 类型。

注释

The syntax for commenting in QML is similar to that of JavaScript:

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

Comments are ignored by the engine when processing QML code. They are useful for explaining what a section of code is doing, whether for reference at a later date or for explaining the implementation to others.

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

Text {
    text: "Hello world!"
    //opacity: 0.5
}
					

在以上范例中, Text object will have normal opacity, since the line opacity: 0.5 has been turned into a comment.