基础知识

QML提供了一种灵活的锚定位方式来布局。锚定的概念是Item的基础,并且适用于所有可视化的QML元素。锚的作用像一个契约,比竞争的几何变化更强大。锚是相对表达的,您总是需要一个相关的元素来进行锚定。
image.png
一个元素有 6 条主要的锚线(top, bottom,left, right, horizontalCenter, verticalCenter)。此外,Text 元素中还有用于文本的基线锚点。每条锚线都有一个偏移量。在top, bottom, left, 和right锚点的情况下,它们称为边距。对于horizontalCenter, verticalCenter and baseline,它们被称为偏移量。 :::success 在源码中我们增加了MouseArea。增加的作用是为了让元素正常情况下可以被移动。但是加入锚布局以后,是个什么样子呢,我们一起来看看。 ::: 基础组件的源码:

  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. property real mouse_x: 0
  10. property real mouse_y: 0
  11. Text {
  12. id: label
  13. anchors.centerIn: parent
  14. text:root.showText
  15. }
  16. MouseArea{
  17. anchors.fill: parent
  18. onPressed: {
  19. mouse_x = mouseX
  20. mouse_y = mouseY
  21. }
  22. onPositionChanged: {
  23. root.x = mouseX + root.x - mouse_x
  24. root.y = mouseY + root.y - mouse_y
  25. }
  26. }
  27. border.color: Qt.lighter(color)
  28. }
  29. //GreenSquare.qml
  30. import QtQuick
  31. Rectangle {
  32. id:root
  33. width: 48
  34. height: 48
  35. color: "#228B22"
  36. property string showText: ""
  37. property real mouse_x: 0
  38. property real mouse_y: 0
  39. Text {
  40. id: label
  41. anchors.centerIn: parent
  42. text:root.showText
  43. }
  44. MouseArea{
  45. anchors.fill: parent
  46. onPressed: {
  47. mouse_x = mouseX
  48. mouse_y = mouseY
  49. }
  50. onPositionChanged: {
  51. root.x = mouseX + root.x - mouse_x
  52. root.y = mouseY + root.y - mouse_y
  53. }
  54. }
  55. border.color: Qt.lighter(color)
  56. }
  57. //BlueSquare.qml
  58. import QtQuick
  59. Rectangle {
  60. id:root
  61. width: 48
  62. height: 48
  63. color: "#1E90FF"
  64. property string showText: ""
  65. property real mouse_x: 0
  66. property real mouse_y: 0
  67. Text {
  68. id: label
  69. anchors.centerIn: parent
  70. text:root.showText
  71. }
  72. MouseArea{
  73. anchors.fill: parent
  74. onPressed: {
  75. mouse_x = mouseX
  76. mouse_y = mouseY
  77. }
  78. onPositionChanged: {
  79. root.x = mouseX + root.x - mouse_x
  80. root.y = mouseY + root.y - mouse_y
  81. }
  82. }
  83. border.color: Qt.lighter(color)
  84. }
  1. 父元素填充子元素 ```ruby import QtQuick

GreenSquare { BlueSquare { width: 12 anchors.fill: parent anchors.margins: 8 showText: ‘(1)’ } }

  1. 结果:<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/214918/1636035942775-12e2912b-a406-43e5-a19b-2378d8d15132.png#clientId=ued7be6b1-e87b-4&from=paste&height=207&id=BFbHa&margin=%5Bobject%20Object%5D&name=image.png&originHeight=207&originWidth=238&originalType=binary&ratio=1&size=2535&status=done&style=stroke&taskId=uce54d2b2-1dd3-4ea8-b97e-5e02383868e&width=238)
  2. 2. 元素与父元素左对齐。
  3. ```ruby
  4. // RepeaterExample.qml
  5. import QtQuick
  6. GreenSquare {
  7. BlueSquare {
  8. width: 48
  9. y: 8
  10. anchors.left: parent.left
  11. anchors.leftMargin: 8
  12. showText: "(2)"
  13. }
  14. }

结果:
image.png

  1. 元素的左侧与父元素的右侧对齐。 ```ruby

import QtQuick Rectangle{ GreenSquare { width: 120 height: 40 BlueSquare { width: 48 anchors.left: parent.right showText: ‘(3)’ } } }

  1. 结果:<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/214918/1636036380390-8d3c0fd3-af3f-4329-bb96-159038c1cf71.png#clientId=ued7be6b1-e87b-4&from=paste&height=163&id=ube16512a&margin=%5Bobject%20Object%5D&name=image.png&originHeight=163&originWidth=321&originalType=binary&ratio=1&size=2589&status=done&style=stroke&taskId=u063b5f37-3dc1-4caf-bc12-67d59543cf1&width=321)
  2. 4. 居中对齐元素。`Blue1` 水平居中于父级。`Blue2 `也水平居中,但在 `Blue 1` 上,其顶部与 `Blue1` 底线对齐。
  3. ```ruby
  4. import QtQuick
  5. GreenSquare {
  6. BlueSquare {
  7. id: blue1
  8. width: 48; height: 24
  9. y: 8
  10. anchors.horizontalCenter: parent.horizontalCenter
  11. }
  12. BlueSquare {
  13. id: blue2
  14. width: 72; height: 24
  15. anchors.top: blue1.bottom
  16. anchors.topMargin: 4
  17. anchors.horizontalCenter: blue1.horizontalCenter
  18. showText: '(4)'
  19. }
  20. }

结果:
image.png

  1. 元素以父元素为中心

    1. import QtQuick
    2. GreenSquare {
    3. BlueSquare {
    4. width: 48
    5. anchors.centerIn: parent
    6. showText: '(5)'
    7. }
    8. }

    结果:
    image.png

  2. 使用水平和垂直中线,元素以父元素的左偏移居中

    1. import QtQuick
    2. GreenSquare {
    3. BlueSquare {
    4. width: 48
    5. anchors.horizontalCenter: parent.horizontalCenter
    6. anchors.horizontalCenterOffset: -12
    7. anchors.verticalCenter: parent.verticalCenter
    8. showText: '(6)'
    9. }
    10. }

    结果:
    image.png

    再加点料

    在子元素中我们增加了MouseArea区域,支持对子元素的拖动。试试这个例子,拖动一些方块。您将看到:

  • (1)不能被拖动,因为它是锚定在所有的方面(尽管您可以拖动(1)的父级,因为它根本没有被锚定)。
  • (2)可以垂直拖动,因为只有左侧被锚定。
  • 这同样适用于(3)。(4)只能垂直拖动,因为两个方块都是水平居中的。
  • (5)以父方为中心,因此不能被拖拽。
  • 这同样适用于(7)。拖动一个元素意味着改变它的x、y位置。

由于锚定比设置x、y属性更强,拖动受到锚定线的限制。我们将在稍后讨论动画时看到这个效果。