• QT之QML开发 锚点布局


    QML中经常会用到锚点布局,本篇简单演示一下锚点布局的使用。

    目录

    1.锚点布局的说明

    2.锚点布局三等分窗口

    3.锚点布局拆分窗口

    4.窗口拖动

    5.完整代码


    1.锚点布局的说明

    2.锚点布局三等分窗口

    main.qml

    1. import QtQuick 2.15
    2. import QtQuick.Window 2.15
    3. import QtQuick.Controls 2.15
    4. // 别名必须大写,小写会报错:Invalid import qualifier ID
    5. import './index.js' as MyJs
    6. Window {
    7. width: 640
    8. height: 480
    9. visible: true
    10. title: qsTr("Hello World")
    11. Item {
    12. //锚点填充
    13. anchors.fill: parent
    14. Rectangle {
    15. id: rect1
    16. anchors.left: parent.left
    17. anchors.top: parent.top
    18. anchors.bottom: parent.bottom
    19. width: parent.width / 3
    20. color: "black"
    21. }
    22. Rectangle {
    23. id: rect2
    24. anchors.left: rect1.right
    25. anchors.top: parent.top
    26. anchors.bottom: parent.bottom
    27. width: parent.width / 3
    28. color: "red"
    29. }
    30. Rectangle {
    31. id: rect3
    32. anchors.left: rect2.right
    33. anchors.top: parent.top
    34. anchors.bottom: parent.bottom
    35. anchors.right: parent.right
    36. //width: parent.width / 3
    37. color: "yellow"
    38. }
    39. }
    40. }

    3.锚点布局拆分窗口

    main.qml

    1. import QtQuick 2.15
    2. import QtQuick.Window 2.15
    3. import QtQuick.Controls 2.15
    4. // 别名必须大写,小写会报错:Invalid import qualifier ID
    5. import './index.js' as MyJs
    6. Window {
    7. width: 640
    8. height: 480
    9. visible: true
    10. title: qsTr("Hello World")
    11. Item {
    12. //锚点填充
    13. anchors.fill: parent
    14. Rectangle {
    15. anchors.fill: parent
    16. anchors.topMargin: 100
    17. anchors.leftMargin: 50
    18. id: rect1
    19. color: "red"
    20. Component.onCompleted: {
    21. console.log(rect1.width, ' ', rect1.height, ' ', rect1.x, " ", rect1.y)
    22. }
    23. }
    24. Rectangle {
    25. id: rect2
    26. anchors.top: parent.top
    27. anchors.left: parent.left
    28. anchors.right: parent.right
    29. anchors.bottom: rect1.top
    30. color: "blue"
    31. }
    32. Rectangle {
    33. id: rect3
    34. anchors.top: rect2.bottom
    35. anchors.left: parent.left
    36. anchors.right: rect1.left
    37. anchors.bottom: parent.bottom
    38. color: "green"
    39. }
    40. }
    41. }

    4.窗口拖动

    index.js

    1. // qv4 - 只支持es5,不支持es6
    2. function Cal(num) {
    3. this.Num = num
    4. this.add = function(data) {
    5. return this.Num + data
    6. }
    7. this.sub = function(data) {
    8. return this.Num - data
    9. }
    10. }
    11. // Math.random()*n : 生成[0, n)之间的随机数
    12. function getRandomColor(n) {
    13. // 向下取整
    14. return Math.floor(Math.random() * n)
    15. }
    16. function getColorObj() {
    17. let red = getRandomColor(256)
    18. let green = getRandomColor(256)
    19. let blue = getRandomColor(256)
    20. return {red: red / 255, green: green / 255, blue: blue / 255}
    21. }

    main.qml

    1. import QtQuick 2.15
    2. import QtQuick.Window 2.15
    3. import QtQuick.Controls 2.15
    4. // 别名必须大写,小写会报错:Invalid import qualifier ID
    5. import './index.js' as MyJs
    6. Window {
    7. width: 640
    8. height: 480
    9. visible: true
    10. title: qsTr("Hello World")
    11. // Column是不可见元素
    12. Column {
    13. id: col1
    14. Button {
    15. text: "你好啊"
    16. //槽函数
    17. onClicked: {
    18. console.log("onClicked")
    19. console.log(add(111, 222))
    20. let my = new MyJs.Cal(1000)
    21. console.log(my.add(1))
    22. console.log(my.sub(1))
    23. rect.color = "yellow"
    24. let rgba = MyJs.getColorObj()
    25. rect.color = Qt.rgba(rgba.red, rgba.green, rgba.blue, 1.0)
    26. }
    27. function add(data1, data2) {
    28. return data1 + data2;
    29. }
    30. }
    31. Label {
    32. id: labl
    33. text: "Label"
    34. color: "blue"
    35. // 鼠标拖拽
    36. MouseArea {
    37. anchors.fill: parent
    38. drag.target: col1 //labl
    39. }
    40. }
    41. }
    42. // x y z布局,x y是相对于父窗口的位置
    43. // z=0 1 2 3 ... 越大越在上面
    44. Rectangle {
    45. id: rect
    46. x: col1.x
    47. y: col1.y + col1.height
    48. width: 100
    49. height: 100
    50. color: "red"
    51. Button {
    52. x: 0
    53. y: 0
    54. text: "clieck 1"
    55. }
    56. Button {
    57. x: 0
    58. y: 50
    59. text: "clieck 2"
    60. }
    61. }
    62. /*
    63. Rectangle {
    64. id: rect1
    65. x: parent.width - 100
    66. y: 0
    67. width: 100
    68. height: 100
    69. color: "yellow"
    70. }
    71. */
    72. // 锚点布局(anchor-layout) - 另一个:x y z直接定位布局
    73. // 任何可视化布局有x y z height width
    74. //
    75. Item {
    76. //锚点填充
    77. anchors.fill: parent
    78. /*
    79. Rectangle {
    80. id: rect2
    81. //锚定位
    82. // 靠右
    83. //anchors.right: parent.right
    84. // 上下左右居中
    85. // anchors.centerIn: parent
    86. // 靠左居中
    87. anchors.left: parent.left
    88. anchors.verticalCenter: parent.verticalCenter
    89. width: 100
    90. height: 100
    91. color: "black"
    92. }
    93. */
    94. }
    95. }

    5.完整代码

    SimpleQml.pro

    1. QT += quick
    2. CONFIG += c++17
    3. # You can make your code fail to compile if it uses deprecated APIs.
    4. # In order to do so, uncomment the following line.
    5. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
    6. SOURCES += \
    7. main.cpp
    8. RESOURCES += qml.qrc
    9. # Additional import path used to resolve QML modules in Qt Creator's code model
    10. QML_IMPORT_PATH =
    11. # Additional import path used to resolve QML modules just for Qt Quick Designer
    12. QML_DESIGNER_IMPORT_PATH =
    13. # Default rules for deployment.
    14. qnx: target.path = /tmp/$${TARGET}/bin
    15. else: unix:!android: target.path = /opt/$${TARGET}/bin
    16. !isEmpty(target.path): INSTALLS += target

    main.cpp

    1. #include
    2. #include
    3. int main(int argc, char *argv[])
    4. {
    5. #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    6. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    7. #endif
    8. QGuiApplication app(argc, argv);
    9. QQmlApplicationEngine engine;
    10. const QUrl url(QStringLiteral("qrc:/main.qml"));
    11. QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
    12. &app, [url](QObject *obj, const QUrl &objUrl) {
    13. if (!obj && url == objUrl)
    14. QCoreApplication::exit(-1);
    15. }, Qt::QueuedConnection);
    16. engine.load(url);
    17. return app.exec();
    18. }
  • 相关阅读:
    python字符串插入变量的多种方法
    探究美颜算法:直播实时美颜SDK的集成和定制
    [附源码]计算机毕业设计springboot室内设计类网站
    “蔚来杯“2022牛客暑期多校训练营6 C题: Forest
    数仓面经大框架
    python+django+vue电影票订购系统dyvv4
    Swagger enum 最佳实践:深度剖析与应用指南
    【Linux】如何在进程间加锁(实现互斥)
    成语词典查询易语言代码
    【AI视野·今日Sound 声学论文速览 第九期】Thu, 21 Sep 2023
  • 原文地址:https://blog.csdn.net/hsy12342611/article/details/132956931