基础说明

有许多QML元素可用于项目定位。这些称为定位器,其中QtQuick模块提供以下内容:RowColumnGridFlow。在下面的插图中可以看到它们显示了相同的内容。 :::success 我们在详细介绍之前,让我介绍一些辅助元素:红色蓝色绿色较亮较暗的方块。这些组件中的每一个都包含一个48x48像素凸起的矩形。作为参考,这里是RedSquare的源代码: :::

  1. // RedSquare.qml
  2. import QtQuick
  3. Rectangle {
  4. width: 48
  5. height: 48
  6. color: "#ea7025"
  7. border.color: Qt.lighter(color)
  8. }
  9. //GreenSquare.qml
  10. import QtQuick
  11. Rectangle {
  12. width: 48
  13. height: 48
  14. color: "#228B22"
  15. border.color: Qt.lighter(color)
  16. }
  17. //BlueSquare.qml
  18. import QtQuick
  19. Rectangle {
  20. width: 48
  21. height: 48
  22. color: "#1E90FF"
  23. border.color: Qt.lighter(color)
  24. }

请注意,在填充颜色的基础上,使用Qt.llighter(color)来产生一个较浅的边框颜色。在下面的示例中,我们将使用这些辅助程序,使源代码更加紧凑和可读。请记住,每个矩形最初是48x48像素。

Column定位

  1. // ColumnExample.qml
  2. import QtQuick
  3. Rectangle {
  4. id: root
  5. width: 120
  6. height: 240
  7. Column {
  8. id: row
  9. anchors.centerIn: parent
  10. spacing: 8
  11. RedSquare { }
  12. GreenSquare { width: 96 }
  13. BlueSquare { }
  14. }
  15. }

结果:
image.png

Row定位

Row元素将其子项目彼此相邻放置,根据layoutDirectio属性从左到右或从右到左。同样,spacing用于分隔子项。

  1. // RowExample.qml
  2. import QtQuick
  3. Rectangle {
  4. id: root
  5. width: 400; height: 120
  6. Row {
  7. id: row
  8. anchors.centerIn: parent
  9. spacing: 20
  10. BlueSquare { }
  11. GreenSquare { }
  12. RedSquare { }
  13. }
  14. }

结果:
image.png

Grid定位

Grid元素在网格中排列它的子元素。通过设置RowColumns属性,可以限制行或列的数量。如果不设置它们中的任何一个,则另一个将根据子项目的数量计算。
例如,将行设置为3并添加6个子项将生成2列。属性flowlayoutDirection用于控制将项目添加到网格中的顺序,而spacing控制子项目之间的间距。

  1. // ColumnExample.qml
  2. import QtQuick
  3. Rectangle {
  4. id: root
  5. width: 160
  6. height: 160
  7. Grid {
  8. id: grid
  9. rows: 2
  10. columns: 2
  11. anchors.centerIn: parent
  12. spacing: 8
  13. RedSquare { }
  14. RedSquare { }
  15. RedSquare { }
  16. RedSquare { }
  17. }
  18. }

结果:
image.png

Flow定位

最后的定位器是Flow。它将其子项添加到流中。使用flowlayoutDirection控制流的方向。它可以横着布局,也可以从上到下。
它也可以从左向右或相反的方向运行。在流中添加项时,将根据需要对它们进行包装以形成新的行或列。为了让流工作,它必须有一个宽度或高度。这可以直接设置,或通过锚布局。 :::success 子元素Sequare做了简单更改,增加了序号,让特性显示的更明显。这里仅列出RedSquare.qml,其他两个也做类型更改,增加Text设置 :::

  1. //RedSquare.qml
  2. import QtQuick
  3. Rectangle {
  4. id:root
  5. width: 48
  6. height: 48
  7. color: "#8B0000"
  8. property string showText: ""
  9. Text {
  10. id: label
  11. anchors.centerIn: parent
  12. text:root.showText
  13. }
  14. border.color: Qt.lighter(color)
  15. }
  1. // ColumnExample.qml
  2. import QtQuick
  3. Rectangle {
  4. id: root
  5. width: 160
  6. height: 160
  7. Flow {
  8. anchors.fill: parent
  9. anchors.margins: 20
  10. spacing: 20
  11. RedSquare {showText:"1" }
  12. BlueSquare { showText:"2" }
  13. GreenSquare { showText:"3" }
  14. BlueSquare { showText:"4" }
  15. GreenSquare { showText:"5" }
  16. RedSquare {showText:"6" }
  17. BlueSquare { showText:"7" }
  18. }
  19. }

结果:
动画.gif

Repeater

中继器经常和定位器一起使用。它的工作方式类似于for循环,在模型上进行迭代。在最简单的情况下,模型只是一个提供循环次数的值。

  1. // RepeaterExample.qml
  2. import QtQuick
  3. DarkSquare {
  4. id: root
  5. width: 252
  6. height: 252
  7. property variant colorArray: ["#00bde3", "#67c111", "#ea7025"]
  8. Grid{
  9. anchors.fill: parent
  10. anchors.margins: 8
  11. spacing: 4
  12. Repeater {
  13. model: 16
  14. delegate: Rectangle {
  15. required property int index
  16. property int colorIndex: Math.floor(Math.random()*3)
  17. width: 56; height: 56
  18. color: root.colorArray[colorIndex]
  19. border.color: Qt.lighter(color)
  20. Text {
  21. anchors.centerIn: parent
  22. color: "#f0f0f0"
  23. text: "Cell " + parent.index
  24. }
  25. }
  26. }
  27. }
  28. }

结果:
image.png
在这个中继器示例中,我们使用了一些新的魔法。我们定义了自己的颜色数组属性,它是一个颜色数组。中继器创建一系列矩形(由模型定义16 个)。
对于每个循环,它创建由转发器的子节点定义的矩形。在矩形中,我们使用 JS 数学函数选择颜色:Math.floor(Math.random()\*3)。这为我们提供了一个范围为0..2 的随机数,我们用它来从颜色数组中选择颜色。如前所述,Java ScriptQt Quick 的核心部分,因此我们可以使用标准库。
转发器将index属性注入转发器。它包含当前循环索引。(0,1,..15)。我们可以使用它根据索引做出我们自己的决定,或者在我们的例子中使用 Text元素可视化当前索引。 :::success 虽然index属性被动态注入到Rectangle中,但最好将其声明为必需属性,以简化可读性和帮助工具。这是通过必需的属性required property int index实现的。 ::: :::success 使用更高级的动态委托处理更大的模型和动态视图将在模型视图一章中介绍。当有少量静态数据要显示时,最好使用中继器。 :::