当前位置:首页 > 通信资讯 > 正文

qt与qml(qt之qml开发优缺点)

本文介绍QT QML跨平台移动APP开发中的元素布局的相关问题,先看一张图,我们来分析一下其中的问题:

qt与qml(qt之qml开发优缺点)

这张图片中,有如下问题:

整体的布局没有居中显示
班级名称:
没有和 请输入班级名称输入框垂直对齐
和输入框的距离太远
班主任的提示也一样
最后的Button一行,需求要求右对齐,在QML的程序中没有实现

代码修改完以后的效果:

qt与qml(qt之qml开发优缺点)

改变宽度试一下:

qt与qml(qt之qml开发优缺点)

原代码说明:

main.qml

  1. import QtQuick 2.12
  2. import QtQuick.Window 2.12
  3. Window {
  4. visible: true
  5. width: 640
  6. height: 480
  7. title: qsTr("QML 元素布局")
  8. InputPage{
  9. // 充满父类
  10. anchors.fill: parent
  11. // 设置margins
  12. anchors.margins: 10
  13. }
  14. }

InputPage.qml

  1. import QtQuick 2.0
  2. import QtQuick.Controls 2.12
  3. Page {
  4. // 定义参数,每行的高度
  5. property int rowHeight: 40
  6. // 定义参数,每行中,每列的间距
  7. property int rowSpacing: 8
  8. // 定义一列
  9. Column{
  10. id: column
  11. // 充满父类Page类
  12. anchors.fill: parent
  13. // 定义Column中,每行Row的间距
  14. spacing: 10
  15. Row{
  16. // 宽度去Page的0.8
  17. width: parent.width * 0.8
  18. height: rowHeight
  19. spacing: rowSpacing
  20. // Row水平居中显示
  21. anchors.horizontalCenter: parent.horizontalCenter
  22. Label{
  23. text: "班级名称"
  24. // 定义垂直居中显示
  25. verticalAlignment: className.verticalAlignment
  26. // 显示字符,水平靠右显示
  27. horizontalAlignment: Text.AlignRight
  28. // 设置宽度,Row的宽度的0.3
  29. width: parent.width * 0.3
  30. height: parent.height
  31. }
  32. TextField{
  33. id: className
  34. placeholderText: "请输入班级名称"
  35. // 设置宽度,Row的宽度的0.60
  36. width: parent.width * 0.60
  37. height: parent.height
  38. }
  39. }
  40. // 同上一行代码
  41. Row{
  42. width: parent.width * 0.8
  43. height: rowHeight
  44. spacing: rowSpacing
  45. anchors.horizontalCenter: parent.horizontalCenter
  46. Label{
  47. text: "班主任"
  48. verticalAlignment: teacherInChargeClass.verticalAlignment
  49. horizontalAlignment: Text.AlignRight
  50. width: parent.width * 0.3
  51. height: parent.height
  52. }
  53. TextField{
  54. id: teacherInChargeClass
  55. placeholderText: "请输入班主任姓名"
  56. width: parent.width * 0.6
  57. height: parent.height
  58. }
  59. }
  60. Row{
  61. width: parent.width * 0.8
  62. height: rowHeight
  63. spacing: rowSpacing
  64. anchors.horizontalCenter: parent.horizontalCenter
  65. // 设置Button一行的左侧的充满宽度
  66. Label{
  67. text: ""
  68. // 宽度说明
  69. // 上述两行(班级名称,班主任)的总宽度是id=column的宽度的0.9倍
  70. // 三个Button的宽度 = b1.width*3
  71. // 三个Button的宽度,其中间的间隔有两个间隔宽度
  72. // 所以本行的宽度和上两行的宽度是一致的,这样就保证了button右对齐的
  73. width: parent.width * 0.9 - b1.width*3 - rowSpacing*2
  74. height: parent.height
  75. }
  76. Button{
  77. id: b1
  78. text: "新增"
  79. width: parent.width * 0.15
  80. height: parent.height
  81. }
  82. Button{
  83. id: b2
  84. text: "保存"
  85. width: parent.width * 0.15
  86. height: parent.height
  87. }
  88. Button{
  89. id: b3
  90. text: "放弃"
  91. width: parent.width * 0.15
  92. height: parent.height
  93. }
  94. }
  95. }
  96. }

参考课程 《QT QML跨平台移动APP编程》

原文链接:https://blog.csdn.net/jamescat/article/details/104517910

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。