当前位置: 首页 > news >正文

qml之锚点Anchors

一、锚点概念

QML 中的锚点(Anchors)是一种声明式的布局系统,它允许你定义 UI 元素之间的相对位置关系,而不是使用绝对坐标定位。

锚点的核心概念

  1. 关系绑定

    • 锚点不是固定位置,而是建立元素之间的相对关系

    • 类似于"将这个按钮的右边固定在那个文本框的左边"

  2. 动态响应

    • 当被锚定的元素移动或改变大小时,依赖它的元素会自动调整

    • 自动适应不同屏幕尺寸和方向变化

  3. 布局系统

    • 比传统(x,y)坐标定位更高级的抽象

    • 属于声明式布局(描述"什么"而不是"如何")

与绝对定位的对比

特性锚点布局绝对定位
定位方式相对其他元素固定坐标
响应式自动适应变化需要手动调整
复杂度声明简单关系需要精确计算
维护性高(关系明确)低(硬编码值)
性能优化过的求解直接赋值

 二、锚点 (Anchors) 使用

锚布局(Anchors)是QML中一种通过指定元素之间的相对位置关系来确定元素在界面中位置的布局方式。它允许你定义一个元素如何相对于其父元素或其他元素进行定位,例如,你可以指定一个元素的左边与父元素的左边对齐,或者一个元素的顶部与另一个元素的底部对齐等。

  1. 基本锚点

    • anchors.left - 左边缘

    • anchors.right - 右边缘

    • anchors.top - 顶部边缘

    • anchors.bottom - 底部边缘

    • anchors.horizontalCenter - 水平中心

    • anchors.verticalCenter - 垂直中心

    • anchors.baseline - 文本基线,是用于定位文本的,对于没有文本的图元,baseline和top一致。

  2. 边距

    • anchors.leftMargin - 左边缘边距

    • anchors.rightMargin - 右边缘边距

    • anchors.topMargin - 顶部边距

    • anchors.bottomMargin - 底部边距

    • anchors.horizontalCenterOffset - 水平中心偏移

    • anchors.verticalCenterOffset - 垂直中心偏移

    • anchors.centerIn  - 用于将一个元素居中于另一个元素或父元素。

  3. 填充

    • anchors.fill: parent - 填充整个父元素

    • anchors.margins - 设置所有边距的统一值

 

三、示例代码

示例1:基本锚定

Rectangle {
    id: parentRect
    width: 200; height: 200
    color: "lightblue"
    
    Rectangle {
        id: childRect
        width: 100; height: 50
        color: "salmon"
        // 锚定到父元素的中心
        anchors.centerIn: parent
    }
}

 

示例2:边距使用

Rectangle {
    id: container
    width: 300; height: 200
    color: "lightgray"
    
    Rectangle {
        id: box
        color: "navy"
        // 锚定到父元素,但留有10像素的边距
        anchors.fill: parent
        anchors.margins: 10
    }
}

示例3:填充

    Rectangle {
        width: 200; height: 200
        color: "lightblue"

        Rectangle {
            width: 100; height: 100
            anchors.leftMargin:20
            color: "salmon"
            anchors.fill: parent  // 填充整个父元素
        }
    }

 

示例4:元素占据右侧 20% 区域

 

示例5:复杂锚定

Rectangle {
    width: 400; height: 400
    color: "white"
    
    Rectangle {
        id: leftColumn
        width: 100; color: "lightgreen"
        anchors {
            top: parent.top
            bottom: parent.bottom
            left: parent.left
            topMargin: 20
            bottomMargin: 20
        }
    }
    
    Rectangle {
        id: rightItem
        height: 50; color: "orange"
        anchors {
            top: parent.top
            right: parent.right
            left: leftColumn.right
            margins: 10
        }
    }
}

相关文章:

  • Google Cloud Next‘25大会 Gemini 支持 Anthropic MCP 协议及推出 A2A 协议剑指医疗AI情况分析
  • QBitmap、QPixmap、QImage 和 QPicture 使用方法和特点以及转换
  • Windows10 ssh无输出 sshd服务启动失败 1067报错 公钥无法认证链接 解决办法
  • Android 中绕过hwbinder 实现跨模块对audio 的HAL调用
  • Java面试黄金宝典45
  • POSIX线程(pthread)库:线程的终止与管理
  • C#异步方法返回Task<T>的同步调用
  • LLM相关代码笔记
  • 【Docker基础】容器技术详解:生命周期、命令与实战案例
  • Java网络编程实战(多人聊天室-CS模式)
  • ollama加载本地自定义模型
  • 在 Linux 系统(ubuntu/kylin)上安装 Docker
  • 玻璃厂退火炉“温度智囊”:Profinet转ModbusRTU网关
  • 目标检测YOLO实战应用案例100讲- 基于卷积神经网络的小目标检测算法研究与应用
  • 灵霄破茧:仙途启幕 - 灵霄门新篇-(4)
  • linux环境定时重启服务的流程分享
  • 关于FocalLoss 损失函数
  • 【C++算法】54.链表_合并 K 个升序链表
  • Ansible:role企业级实战
  • 4-6记录(B树)