QML教程(六)布局

ゝ一世哀愁。 2024-03-30 13:51 123阅读 0赞

目录

一、手动布局

二、锚

三、定位器

Column 将其子级放置在列中

Flow 并排放置其子项,必要时进行换行

Grid 将其子项定位在网格形成中

LayoutMirroring 用于镜像布局行为的属性

Positioner 提供附加属性,这些属性包含有关项在定位器中存在的位置的详细信息

Row 将其子项放置在一排中

四、布局

GridLayout

RowLayout

ColumnLayout


一、手动布局

通过设置项目的 x,y 属性,可以将项目放置在屏幕上的特定 x,y 坐标处。这将根据视觉坐标系规则设置其相对于父项左上角的位置。

结合对这些属性使用绑定而不是常量值,还可以通过将 x 和 y 坐标设置为适当的绑定来轻松实现相对定位。

  1. import QtQuick
  2. Item {
  3. width: 100; height: 100
  4. Rectangle {
  5. // Manually positioned at 20,20
  6. x: 20
  7. y: 20
  8. width: 80
  9. height: 80
  10. color: "red"
  11. }
  12. }

二、锚

该类型提供定位到其他项类型的能力。每个项目有七条锚线:左、右、垂直中心、顶部、底部、基线和水平中心。三条垂直锚点线可以锚定到另一个项目的三个垂直锚点线中的任何一条,四个水平锚点线可以锚定到另一个项目的水平锚点线。

  1. import QtQuick
  2. Item {
  3. width: 200; height: 200
  4. Rectangle {
  5. // Anchored to 20px off the top right corner of the parent
  6. anchors.right: parent.right
  7. anchors.top: parent.top
  8. anchors.margins: 20 // Sets all margins at once
  9. width: 80
  10. height: 80
  11. color: "orange"
  12. }
  13. Rectangle {
  14. // Anchored to 20px off the top center corner of the parent.
  15. // Notice the different group property syntax for 'anchors' compared to
  16. // the previous Rectangle. Both are valid.
  17. anchors { horizontalCenter: parent.horizontalCenter; top: parent.top; topMargin: 20 }
  18. width: 80
  19. height: 80
  20. color: "green"
  21. }
  22. }

三、定位器

对于想要以常规模式定位一组类型的常见情况,Qt Quick提供了一些定位器类型。放置在定位器中的物品会以某种方式自动定位;例如,“行”将项目定位为水平相邻(形成一行)。

  1. Column 将其子级放置在列中

    1. import QtQuick 2.0
    2. Item {
    3. width: 310; height: 170
    4. Column {
    5. anchors.horizontalCenter: parent.horizontalCenter
    6. anchors.verticalCenter: parent.verticalCenter
    7. spacing: 5
    8. Rectangle { color: "lightblue"; radius: 10.0
    9. width: 300; height: 50
    10. Text { anchors.centerIn: parent
    11. font.pointSize: 24; text: "Books" } }
    12. Rectangle { color: "gold"; radius: 10.0
    13. width: 300; height: 50
    14. Text { anchors.centerIn: parent
    15. font.pointSize: 24; text: "Music" } }
    16. Rectangle { color: "lightgreen"; radius: 10.0
    17. width: 300; height: 50
    18. Text { anchors.centerIn: parent
    19. font.pointSize: 24; text: "Movies" } }
    20. }
    21. }
  2. Flow 并排放置其子项,必要时进行换行

    1. import QtQuick 2.0
    2. Rectangle {
    3. color: "lightblue"
    4. width: 300; height: 200
    5. Flow {
    6. anchors.fill: parent
    7. anchors.margins: 4
    8. spacing: 10
    9. Text { text: "Text"; font.pixelSize: 40 }
    10. Text { text: "items"; font.pixelSize: 40 }
    11. Text { text: "flowing"; font.pixelSize: 40 }
    12. Text { text: "inside"; font.pixelSize: 40 }
    13. Text { text: "a"; font.pixelSize: 40 }
    14. Text { text: "Flow"; font.pixelSize: 40 }
    15. Text { text: "item"; font.pixelSize: 40 }
    16. }
    17. }
  3. Grid 将其子项定位在网格形成中

    1. import QtQuick 2.0
    2. Rectangle {
    3. width: 112; height: 112
    4. color: "#303030"
    5. Grid {
    6. anchors.horizontalCenter: parent.horizontalCenter
    7. anchors.verticalCenter: parent.verticalCenter
    8. columns: 2
    9. spacing: 6
    10. Rectangle { color: "#aa6666"; width: 50; height: 50 }
    11. Rectangle { color: "#aaaa66"; width: 50; height: 50 }
    12. Rectangle { color: "#9999aa"; width: 50; height: 50 }
    13. Rectangle { color: "#6666aa"; width: 50; height: 50 }
    14. }
    15. }
  4. LayoutMirroring 用于镜像布局行为的属性

  5. Positioner 提供附加属性,这些属性包含有关项在定位器中存在的位置的详细信息

  6. Row 将其子项放置在一排中

    1. import QtQuick 2.0
    2. Rectangle {
    3. width: 320; height: 110
    4. color: "#c0c0c0"
    5. Row {
    6. anchors.horizontalCenter: parent.horizontalCenter
    7. anchors.verticalCenter: parent.verticalCenter
    8. spacing: 5
    9. Rectangle { width: 100; height: 100; radius: 20.0
    10. color: "#024c1c" }
    11. Rectangle { width: 100; height: 100; radius: 20.0
    12. color: "#42a51c" }
    13. Rectangle { width: 100; height: 100; radius: 20.0
    14. color: "white" }
    15. }
    16. }

四、布局

  1. GridLayout

    1. GridLayout {
    2. id: grid
    3. columns: 3
    4. Text { text: "Three"; font.bold: true; }
    5. Text { text: "words"; color: "red" }
    6. Text { text: "in"; font.underline: true }
    7. Text { text: "a"; font.pixelSize: 20 }
    8. Text { text: "row"; font.strikeout: true }
    9. }
  2. RowLayout

    1. RowLayout {
    2. id: layout
    3. anchors.fill: parent
    4. spacing: 6
    5. Rectangle {
    6. color: 'teal'
    7. Layout.fillWidth: true
    8. Layout.minimumWidth: 50
    9. Layout.preferredWidth: 100
    10. Layout.maximumWidth: 300
    11. Layout.minimumHeight: 150
    12. Text {
    13. anchors.centerIn: parent
    14. text: parent.width + 'x' + parent.height
    15. }
    16. }
    17. Rectangle {
    18. color: 'plum'
    19. Layout.fillWidth: true
    20. Layout.minimumWidth: 100
    21. Layout.preferredWidth: 200
    22. Layout.preferredHeight: 100
    23. Text {
    24. anchors.centerIn: parent
    25. text: parent.width + 'x' + parent.height
    26. }
    27. }
    28. }
  3. ColumnLayout

    1. ColumnLayout{
    2. spacing: 2
    3. Rectangle {
    4. Layout.alignment: Qt.AlignCenter
    5. color: "red"
    6. Layout.preferredWidth: 40
    7. Layout.preferredHeight: 40
    8. }
    9. Rectangle {
    10. Layout.alignment: Qt.AlignRight
    11. color: "green"
    12. Layout.preferredWidth: 40
    13. Layout.preferredHeight: 70
    14. }
    15. Rectangle {
    16. Layout.alignment: Qt.AlignBottom
    17. Layout.fillHeight: true
    18. color: "blue"
    19. Layout.preferredWidth: 70
    20. Layout.preferredHeight: 40
    21. }
    22. }

发表评论

表情:
评论列表 (有 0 条评论,123人围观)

还没有评论,来说两句吧...

相关阅读