附网址:http://qt-project.org/doc/qt-5/qmlfirststeps.html
Creating a QML Document —— 创建一个QML文件
一个QML文件定义了对象的层次结构:具有高度可读的,结构化的布局。每个QML文件由两部分组成:一个引入(import)部分,以及一个对象声明(declaration)部分。用户界面中最常用的的类型(types)和功能由引入QtQuick提供。
Importing and Using the QtQuick Module —— 引入和使用QtQuick模块
为了使用Qt Quick组件,一个QML文件需要引入QtQuick。引入语法类似如下:
import QtQuick 2.0
现在Qt Quick提供的类型和功能可以被用在这个QML文件里了~
Defining an Object Hierarchy —— 定义一个对象的层次结构
QML文件中的对象声明定义了对象在视景中的显示样式。Qt Quick提供了用于构建所有用户界面的基本构建块,例如显示图像或者文本、或是处理用户输入的对象等。
一个简单对象声明可以是一个中心带有文本的彩色的矩形框:
Rectangle {
width: 200
height: 100
color: "red"
Text {
anchors.centerIn: parent
text: "Hello, World!"
}
}
这里定义了一个对象层次:根对象为Rectangle,它拥有一个孩子Text对象。Text对象的父对象被自动设置为这个Rectangle,类似地,Text对象被添加到这个Rectangle对象的子对象属性中。该工作由QML完成。
Putting it All Together —— 放在一起
上面例子中的Rectangle和Text类型都是由引入的QtQuick提供的。将引入语句和对象声明放在一起,我们就得到了一个完整的QML文件:
import QtQuick 2.0
Rectangle {
width: 200
height: 100
color: "red"
Text {
anchors.centerIn: parent
text: "Hello, World!"
}
}
如果我们可以保存这个文件为"HelloWorld.qml",然后加载并显示它。
qmlscene HelloWorld.qml
你将看到文本"Hello, World"在一个红色矩形的中心。
举例来说,考虑下面这个例子:
import QtQuick 2.0
Rectangle {
width: 200
height: 100
color: "red"
Text {
anchors.centerIn: parent
text: "Hello, World!"
}
MouseArea {
anchors.fill: parent
onClicked: parent.color = "blue"
}
}
这个例子可以被保存为"ClickableHelloWorld.qml"并在qmlscene中运行。无论用户点击这个窗口的什么地方,这个矩形都将由红色变成蓝色。注意这个MouseArea类型在触摸事件发生时也会发出clicked信号,因此这段代码可以同样工作在移动设备上。
用户的键盘输入也能被类似的处理通过一段简单的表达式:
import QtQuick 2.0
Rectangle {
width: 200
height: 100
color: "red"
Text {
anchors.centerIn: parent
text: "Hello, World!"
}
focus: true
Keys.onPressed: {
if (event.key == Qt.Key_Return) {
color = "blue";
event.accepted = true;
}
}
}
通过接受焦点,无论何时返回键按下,矩形框的颜色都将被改变成蓝色。
Property Bindings —— 属性绑定
对象和它们的属性来源于QML文件对图形界面定义的定义。QML语言允许属性通过多种方式互相绑定。从而创造高动态性的用户界面。
在下面的例子中,每个子Rectangle的几何属性都被绑定到父Rectangle上。如果父Rectangle的几何形状改变,由于属性绑定,每个孩子的几何形状也都会自动更新。
Animations —— 动画
属性也可以通过动画动态更新。QtQuick提供了多种动画类型,可以用来动态地改变一个属性的值。在下面的例子中,我们设置了一个动态的属性,并且在Text区域中进行显示
import QtQuick 2.0
Rectangle {
color: "lightgray"
width: 200
height: 200
property int animatedValue: 0
SequentialAnimation on animatedValue {
loops: Animation.Infinite
PropertyAnimation { to: 150; duration: 1000 }
PropertyAnimation { to: 0; duration: 1000 }
}
Text {
anchors.centerIn: parent
text: animatedValue
}
}
所显示的值将在0到150之间周期地变化。
Defining Custom QML Types for Re-use —— 定义可复用的自定义QML类型
在QML中最重要的概念之一就是类型的复用。一个应用程序可能含有多个相同的可视类型(例如,多个按钮),同时QML允许这类事物被定义为可复用的、定制的类型,以减小代码重复性并提高可读性。
例如,想象下开发者在Button.qml文件中定义了一个新的按钮类型:
// Button.qml
import QtQuick 2.0
Rectangle {
width: 100; height: 100
color: "red"
MouseArea {
anchors.fill: parent
onClicked: console.log("Button clicked!")
}
}
这个类型可能在应用程序中被复用多次,像下面这样:
// application.qml
import QtQuick 2.0
Column {
Button { width: 50; height: 50 }
Button { x: 50; width: 100; height: 50; color: "blue" }
Button { width: 50; height: 50; radius: 8 }
}
参考QML Object Attributes来获取关于如何开发你自己的可复用组件的更多信息。
Where to Go from Here —— 接下来的旅程
现在你已经看到了QML的响应机制,是时候开始你的下一步了。以下页面将在你与QML的旅程中引导下你。
QML Applications