Introduction to the QML Language
来源:互联网 发布:科讯cms教程 编辑:程序博客网 时间:2024/05/16 07:09
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:
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:
or you can put multiple properties on a single line:
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.
Comments
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 */
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.
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:
import QtQuick 1.0
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 text1.id 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.
Expressions
JavaScript expressions can be used to assign property values. For example:
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:
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.
Properties
Basic Property Types
QML supports properties of many types (see QML Basic Types). The basic types include int, real, bool, string and color.
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.
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:
Signal handlers are explained further below.
List properties
List properties look like this:
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:
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:
can be simplified to:
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:
or like this:
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 Type.property 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:
Another example of attached properties is the Keys element which attaches properties for handling key presses to any visual Item, for example:
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:
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:
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:
import QtQuick 1.0 Rectangle { width: 200 height: 200 color: "blue" Image { source: "pics/logo.png" anchors.centerIn: parent } }
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:
Rectangle { width: 100 height: 100 }
or you can put multiple properties on a single line:
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.
Comments
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 */
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. 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.
Text { text: "Hello world!" //opacity: 0.5 }
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:
import QtQuick 1.0
Row { Text { id: text1 text: "Hello World" } Text { text: text1.text } }
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 text1.id 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.
Expressions
JavaScript expressions can be used to assign property values. For example:
Item { width: 100 * 3 height: 50 + 22 }
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:
Item { width: 300 height: 300 Rectangle { width: parent.width - 50 height: 100 color: "yellow" } }
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.
Properties
Basic Property Types
QML supports properties of many types (see QML Basic Types). The basic types include int, real, bool, string and color.
Item { x: 10.5 // a 'real' property state: "details" // a 'string' property focus: true // a 'bool' property // ... }
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.
Item { x: "hello" // illegal! }
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:
Rectangle { width: 100; height: 100 onWidthChanged: console.log("Width has changed to:", width) onColorChanged: console.log("Color has changed to:", color) }
Signal handlers are explained further below.
List properties
List properties look like this:
Item { children: [ Image {}, Text {} ] }
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:
Image { children: Rectangle {} }
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:
State { changes: [ PropertyChanges {}, PropertyChanges {} ] }
can be simplified to:
State { PropertyChanges {} PropertyChanges {} }
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:
Text { font.pixelSize: 12 font.bold: true }
or like this:
Text { font { pixelSize: 12; bold: true } }
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 Type.property 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:
Component { id: myDelegate Text { text: "Hello" color: ListView.isCurrentItem ? "red" : "blue" } }
ListView { delegate: myDelegate }
Another example of attached properties is the Keys element which attaches properties for handling key presses to any visual Item, for example:
Item { focus: true Keys.onSelectPressed: console.log("Selected") }
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:
Item { width: 100; height: 100 MouseArea { anchors.fill: parent onClicked: { console.log("mouse button clicked") } } }
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:
MouseArea { acceptedButtons: Qt.LeftButton | Qt.RightButton onPressed: { if (mouse.button == Qt.RightButton) console.log("Right mouse button pressed") } }
- Introduction to the QML Language
- Introduction to the DirectX® 9 High Level Shading Language
- UML basics: An introduction to the Unified Modeling Language
- Introduction to The Objective-C 2.0 Programming Language
- UML basics: An introduction to the Unified Modeling Language
- Introduction to the DirectX 9 High-Level Shader Language
- Android: An introduction to the Edify (Updater-Script) language
- Introduction to AppleScript Language Guide
- 《The Way To Go : A Thorough Introduction To The Go Programming Language》之数组切片
- The Way To Go- A Thorough Introduction To The Go Programming Language.pdf 英文原版 免费下载
- Introduction to SQL: Mastering the Relational Database Language (4th Edition)
- An Elementary Introduction to the Wolfram Language Exercises中无法通过测试的题目
- Introduction to the Optimizer
- Introduction to the AndEngine
- Introduction to Linux Intel Assembly Language
- Introduction to A Prototype-based Language - JavaScript
- Briefing introduction to four programming language concepts
- Introduction to Linux Intel Assembly Language
- C++成员运算符和域运算符
- 显示资源图片
- QML Tutorial
- 《华为研发》阅读 - 11 (中研部组织结构)
- linux下subversion+apache安装
- Introduction to the QML Language
- ribbon界面状态栏显示鼠标坐标
- APK反编译
- 设计模式入门--设计模式学习笔记<一>
- ARP,RARP协议笔记
- 即将逝去的大学生活
- 【翻译】一些有趣的算法代码
- [.NET 基于角色安全性验证] 之三:ASP.NET Forms 身份验证
- 网页登陆 记住密码