• QT之QML开发 行列布局,流布局,网格布局


          本节将演示一下QML布局之行列布局,流布局和网格布局

    目录

    1.行列布局

    1.1一列多行

    1.2 一行多列

    2.流布局

    2.1 从左向右(默认)

    ​编辑

    2.2 从上往下

    3.网格布局


    1.行列布局

    1.1一列多行

    1. // 行列布局
    2. import QtQuick 2.15
    3. import QtQuick.Window 2.15
    4. import QtQuick.Controls 2.15
    5. Window {
    6. width: 640
    7. height: 480
    8. visible: true
    9. title: qsTr("Hello World")
    10. Item {
    11. anchors.fill: parent
    12. // 一列
    13. Column {
    14. spacing: 20
    15. // 两行
    16. Row {
    17. spacing: 20
    18. RadioButton {
    19. checked: true
    20. text: "中国"
    21. }
    22. RadioButton {
    23. text: "美国"
    24. }
    25. RadioButton {
    26. text: "英国"
    27. }
    28. }
    29. Row {
    30. spacing: 20
    31. RadioButton {
    32. checked: true
    33. text: "红色"
    34. }
    35. RadioButton {
    36. text: "绿色"
    37. }
    38. RadioButton {
    39. text: "蓝色"
    40. }
    41. }
    42. }
    43. }
    44. }

    1.2 一行多列

    1. // 行列布局
    2. import QtQuick 2.15
    3. import QtQuick.Window 2.15
    4. import QtQuick.Controls 2.15
    5. Window {
    6. width: 640
    7. height: 480
    8. visible: true
    9. title: qsTr("Hello World")
    10. Item {
    11. anchors.fill: parent
    12. // 一行
    13. Row {
    14. spacing: 20
    15. // 两列
    16. Column {
    17. spacing: 20
    18. RadioButton {
    19. checked: true
    20. text: "中国"
    21. }
    22. RadioButton {
    23. text: "美国"
    24. }
    25. RadioButton {
    26. text: "英国"
    27. }
    28. }
    29. Column {
    30. spacing: 20
    31. RadioButton {
    32. checked: true
    33. text: "红色"
    34. }
    35. RadioButton {
    36. text: "绿色"
    37. }
    38. RadioButton {
    39. text: "蓝色"
    40. }
    41. }
    42. }
    43. }
    44. }

    2.流布局

    2.1 从左向右(默认)

    1. // 流Flow布局
    2. import QtQuick 2.15
    3. import QtQuick.Window 2.15
    4. import QtQuick.Controls 2.15
    5. Window {
    6. width: 640
    7. height: 480
    8. visible: true
    9. title: qsTr("Hello World")
    10. Item {
    11. id: item
    12. anchors.fill: parent
    13. // 流布局: 设定宽高后自动排列
    14. Flow {
    15. anchors.left: parent.left
    16. anchors.right: parent.right
    17. //flow: Flow.LeftToRight // 默认是从左向右
    18. Rectangle {
    19. height: 100
    20. width: item.width / 3
    21. color: "red"
    22. }
    23. Rectangle {
    24. height: 100
    25. width: item.width / 3
    26. color: "green"
    27. }
    28. Rectangle {
    29. height: 100
    30. width: item.width / 3
    31. color: "red"
    32. }
    33. Rectangle {
    34. height: 100
    35. width: item.width / 3
    36. color: "green"
    37. }
    38. }
    39. }
    40. }

    2.2 从上往下

    1. // 流Flow布局
    2. import QtQuick 2.15
    3. import QtQuick.Window 2.15
    4. import QtQuick.Controls 2.15
    5. Window {
    6. width: 640
    7. height: 480
    8. visible: true
    9. title: qsTr("Hello World")
    10. Item {
    11. id: item
    12. anchors.fill: parent
    13. // 流布局: 设定宽高后自动排列
    14. Flow {
    15. anchors.left: parent.left
    16. anchors.right: parent.right
    17. flow: Flow.TopToBottom
    18. Rectangle {
    19. height: 100
    20. width: item.width / 3
    21. color: "red"
    22. }
    23. Rectangle {
    24. height: 100
    25. width: item.width / 3
    26. color: "green"
    27. }
    28. Rectangle {
    29. height: 100
    30. width: item.width / 3
    31. color: "red"
    32. }
    33. Rectangle {
    34. height: 100
    35. width: item.width / 3
    36. color: "green"
    37. }
    38. }
    39. }
    40. }

    3.网格布局

    3行2列布局演示

    1. // 网格布局
    2. import QtQuick 2.15
    3. import QtQuick.Window 2.15
    4. import QtQuick.Controls 2.15
    5. import QtQuick.Layouts 1.0
    6. Window {
    7. width: 640
    8. height: 480
    9. visible: true
    10. title: qsTr("Hello World")
    11. Item {
    12. id: item
    13. anchors.fill: parent
    14. // 网格布局
    15. //GridLayout {
    16. Grid {
    17. id: grid
    18. columns: 2
    19. rows: 3
    20. Text { text: "Three"; font.bold: true; }
    21. Text { text: "words"; color: "red" }
    22. Text { text: "in"; font.underline: true }
    23. Text { text: "a"; font.pixelSize: 20 }
    24. Text { text: "row"; font.strikeout: true }
    25. }
    26. }
    27. }

  • 相关阅读:
    【每日一题Day36】LC1742盒子中小球的最大数量 | 哈希表 找规律
    数据库优化redis【培训结语】
    LeetCode讲解篇之40. 组合总和 II
    请问这个for loop的running time是多少?
    Java · 逻辑控制(顺序结构 · 分支结构 · 循环结构) · 输入输出语句
    网页下拉菜单
    css3加js实现放大镜效果
    BlockCanary原理解析
    关于ASP.NET的ViewState相关学习
    ES6 --》函数扩展以及箭头函数讲解
  • 原文地址:https://blog.csdn.net/hsy12342611/article/details/133295106