基础说明
有许多QML元素可用于项目定位。这些称为定位器,其中QtQuick模块提供以下内容:Row
,Column
,Grid
和Flow
。在下面的插图中可以看到它们显示了相同的内容。
:::success
我们在详细介绍之前,让我介绍一些辅助元素:红色,蓝色,绿色,较亮和较暗的方块。这些组件中的每一个都包含一个48x48像素凸起的矩形。作为参考,这里是RedSquare
的源代码:
:::
// RedSquare.qml
import QtQuick
Rectangle {
width: 48
height: 48
color: "#ea7025"
border.color: Qt.lighter(color)
}
//GreenSquare.qml
import QtQuick
Rectangle {
width: 48
height: 48
color: "#228B22"
border.color: Qt.lighter(color)
}
//BlueSquare.qml
import QtQuick
Rectangle {
width: 48
height: 48
color: "#1E90FF"
border.color: Qt.lighter(color)
}
请注意,在填充颜色的基础上,使用Qt.llighter(color)
来产生一个较浅的边框颜色。在下面的示例中,我们将使用这些辅助程序,使源代码更加紧凑和可读。请记住,每个矩形最初是48x48
像素。
Column定位
// ColumnExample.qml
import QtQuick
Rectangle {
id: root
width: 120
height: 240
Column {
id: row
anchors.centerIn: parent
spacing: 8
RedSquare { }
GreenSquare { width: 96 }
BlueSquare { }
}
}
Row定位
Row
元素将其子项目彼此相邻放置,根据layoutDirectio
属性从左到右或从右到左。同样,spacing
用于分隔子项。
// RowExample.qml
import QtQuick
Rectangle {
id: root
width: 400; height: 120
Row {
id: row
anchors.centerIn: parent
spacing: 20
BlueSquare { }
GreenSquare { }
RedSquare { }
}
}
Grid定位
Grid
元素在网格中排列它的子元素。通过设置Row
和Columns
属性,可以限制行或列的数量。如果不设置它们中的任何一个,则另一个将根据子项目的数量计算。
例如,将行设置为3并添加6个子项将生成2列。属性flow
和layoutDirection
用于控制将项目添加到网格中的顺序,而spacing
控制子项目之间的间距。
// ColumnExample.qml
import QtQuick
Rectangle {
id: root
width: 160
height: 160
Grid {
id: grid
rows: 2
columns: 2
anchors.centerIn: parent
spacing: 8
RedSquare { }
RedSquare { }
RedSquare { }
RedSquare { }
}
}
Flow定位
最后的定位器是Flow
。它将其子项添加到流中。使用flow
和layoutDirection
控制流的方向。它可以横着布局,也可以从上到下。
它也可以从左向右或相反的方向运行。在流中添加项时,将根据需要对它们进行包装以形成新的行或列。为了让流工作,它必须有一个宽度或高度。这可以直接设置,或通过锚布局。
:::success
子元素Sequare做了简单更改,增加了序号,让特性显示的更明显。这里仅列出RedSquare.qml
,其他两个也做类型更改,增加Text
设置
:::
//RedSquare.qml
import QtQuick
Rectangle {
id:root
width: 48
height: 48
color: "#8B0000"
property string showText: ""
Text {
id: label
anchors.centerIn: parent
text:root.showText
}
border.color: Qt.lighter(color)
}
// ColumnExample.qml
import QtQuick
Rectangle {
id: root
width: 160
height: 160
Flow {
anchors.fill: parent
anchors.margins: 20
spacing: 20
RedSquare {showText:"1" }
BlueSquare { showText:"2" }
GreenSquare { showText:"3" }
BlueSquare { showText:"4" }
GreenSquare { showText:"5" }
RedSquare {showText:"6" }
BlueSquare { showText:"7" }
}
}
Repeater
中继器经常和定位器一起使用。它的工作方式类似于for
循环,在模型上进行迭代。在最简单的情况下,模型只是一个提供循环次数的值。
// RepeaterExample.qml
import QtQuick
DarkSquare {
id: root
width: 252
height: 252
property variant colorArray: ["#00bde3", "#67c111", "#ea7025"]
Grid{
anchors.fill: parent
anchors.margins: 8
spacing: 4
Repeater {
model: 16
delegate: Rectangle {
required property int index
property int colorIndex: Math.floor(Math.random()*3)
width: 56; height: 56
color: root.colorArray[colorIndex]
border.color: Qt.lighter(color)
Text {
anchors.centerIn: parent
color: "#f0f0f0"
text: "Cell " + parent.index
}
}
}
}
}
结果:
在这个中继器示例中,我们使用了一些新的魔法。我们定义了自己的颜色数组属性,它是一个颜色数组。中继器创建一系列矩形(由模型定义16 个)。
对于每个循环,它创建由转发器的子节点定义的矩形。在矩形中,我们使用 JS 数学函数选择颜色:Math.floor(Math.random()\*3)
。这为我们提供了一个范围为0..2
的随机数,我们用它来从颜色数组中选择颜色。如前所述,Java Script 是 Qt Quick 的核心部分,因此我们可以使用标准库。
转发器将index
属性注入转发器。它包含当前循环索引。(0,1,..15)
。我们可以使用它根据索引做出我们自己的决定,或者在我们的例子中使用 Text
元素可视化当前索引。
:::success
虽然index
属性被动态注入到Rectangle
中,但最好将其声明为必需属性,以简化可读性和帮助工具。这是通过必需的属性required property int index
实现的。
:::
:::success
使用更高级的动态委托处理更大的模型和动态视图将在模型视图一章中介绍。当有少量静态数据要显示时,最好使用中继器。
:::