Qml基于锚(anchors)的布局

article/2025/11/5 14:47:25

锚(anchors)布局,每一个项目都可以认为有一组无形的锚线

left,horizontalCenter,right,top,verticalCenter,baseline和bottom.

在这里插入图片描述

如上图:图中没有显示baseline,它是一条假想的线,文本坐落在这条线上。对于没有文本的项目它与top相同。

  1. 使用锚布局
    7条锚分别对应了Item项目中的anchors属性组的相关属性。因为Qt Quick中所有可视项目都继承自Item,所以所有可视项目都可以使用锚进行布局。Qt Quick的锚定系统允许不同项目的锚线之间建立关系。
import QtQuick 2.2Item {Rectangle {id: rect1width: 50; height: 50; color: "blue"}Rectangle {id: rect2width: 50; height: 50; color: "red"anchors.left: rect1.right}
}
  • 这里rect2的左边界锚定到了rect1的右边界。

在这里插入图片描述

  • 另外还可以指定多个锚。
Rectangle { id: rect1; .... }
Rectangle {id: rect2;anchors.left; rect1.right; anchors.rop; rect1.bottom; ...
}

在这里插入图片描述

通过指定多个水平或者垂直的锚可以控制一个项目的大小:
例如:rect2锚定到了rect1的右边和rect3的左边,当rect1或rect3移动时,rect2会进行必要的伸缩或收缩。

Rectangle { id: rect1; x: 0; ... }
Rectangle { id: rect2; anchors.left: rect1.right;   anchors.right: rect3.left; ... }
Rectangle { id: rect3; x: 150; ... }

在这里插入图片描述

Qt Quick 还提供了一系列方便使用的锚。例如,使用anchors.fill等价于设置left,right,top和bottom锚定到目标项目的left,right,top和bottom; anchors.centerIn等价于设置verticalCenter和horizontalCenter锚定到目标项目的verticalCenter 和 horizontalCenter等。

  1. 锚边距和位移
    锚定系统也允许为一个项目的锚指定边距(margin)和偏移量(offset)。 边距指定了项目锚到外边界的空间量,而偏移则允许使用中心锚线进行定位。一个项目可以通过leftMargin,rigtMargin,topMargin和bottomMargin独立指定锚边距。

如图所示:也可以使用anchor.margins来为所有的4个边指定相同的边距。锚偏移可以使用
horizontalCenterOffset,verticalCenterOffset 和 baselineOffset来指定。

  • 例如下面代码片段中,在rect2的左边留有5像素的边距。运行效果示意图:
import QtQuick 2.0Item {Rectangle { id: rect1; width: 50; height: 50; color: "blue" }Rectangle {id: rect2;width: 50; height: 50;color: "red"anchors.left: rect1.right;anchors.leftMargin: 5;}
}

在这里插入图片描述

  1. 运行时改变锚
    Qt Quick 提供了AnchorChanges类型,以便在运行时修改项目的锚,它需要在状态State中进行。AnchorChanges不能修改项目的边距,需要时可以使用PropertyChanges完成。
    例子:使用AnchorChanges改变了项目的top和bottom锚,使用PropertyChanges修改了top和bottom锚边距。
import QtQuick 2.2Rectangle {id: windowwidth: 120; height: 120color: "black"Rectangle { id: myRect; width: 50; height: 50; color: "red" }states: State {name: "reanchored"AnchorChanges {target: myRectanchors.top: window.topanchors.bottom: window.bottom}PropertyChanges {target: myRectanchors.topMargin: 10anchors.bottomMargin: 10}}MouseArea {anchors.fill: parent;onClicked: window.state = "reanchored"}
}

在这里插入图片描述


http://chatgpt.dhexx.cn/article/Emf9YMCP.shtml

相关文章

RPN之生成anchors

看不懂anchor生成方法,就看不懂参数。 faster_rcnn的anchors生成 在这里我就不看faster_rcnn的anchors生成源代码了,我们用numpy自己去实现,在128*128大小的图片中生成anchors. 开始上代码 import numpy as np import matplotlib.pyplot …

Qml anchors锚点布局

关于qml的锚点布局的一些分享,并不能算是教学之类的。 锚点布局会自动设置宽度高度,并且优先级比 width height更高 import QtQuick 2.13 import QtQuick.Window 2.12 import QtQml 2.13Window {width: 640height: 480visible: truetitle: qsTr("a…

yolo_anchors.txt超参数值设置

keras-yolov3在训练自定义图片集之前,设置合理的yolo_anchors.txt值,有利于模型训练的收敛,一般都带有默认的参数如下: 在实际项目中,yolo_anchors的值可以根据kmeans.py计算获取,通过聚类得到最佳anchors数…

QML之anchors锚布局

QML的布局方式一般采用两种,一种就是直接设置,X与Y坐标的值。一种是采用相对位置布局,anchors锚布局。使用锚布局的能够使界面更紧凑,更有整体化。我个人相对比较喜欢使用锚布局,选取一个最合适的参考点,其…

YOLOV5源代码学习之check_anchors()函数

该函数主要在train.py中调用 为了方便直观的阅读代码,对代码中的变量值进行了输出 def check_anchors(dataset, model, thr4.0, imgsz640):# Check anchor fit to data, recompute if necessaryprefix colorstr(autoanchor: )print(f\n{prefix}Analyzing anchors.…

QML入门教程(3): anchors的用法

QML的布局方法是anchors,锚。 例如界面上四个按钮,可以用绝对坐标 (x, y) 设置他们的位置, 代码如下: import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.5Window {visible: truewidth: 320height: 480title: qsTr("Hello World")co…

目标检测2——借Detectron2源码理解Anchors与RPN

文章目录 1.Regionn Proposal Network背景2.Regionn Proposal Network的结构3.Anchors4.Regionn Proposal Network的训练参考资料 欢迎访问个人网络日志🌹🌹知行空间🌹🌹 1.Regionn Proposal Network背景 RPN,Region …

QML基础:锚anchors

正文 除了传统的Grid、Row和Column外,Qt Quick还提供了一种使用锚点的方法来布局。可以将每个元素视为具有7条不可见的“锚定线”的集合:left、horizontalCenter、right、top、verticalCenter、baseline和bottom。 baseline(上图未展示)对应于文本所在的虚线。对于没有文…

细说目标检测中的Anchors

本文转载自AI公园。 作者:Raghul Asokan 编译:ronghuaiyang 导读 给大家再次解释一下Anchors在物体检测中的作用。 今天,我将讨论在物体检测器中引入的一个优雅的概念 —— Anchors,它是如何帮助检测图像中的物体,以及…

anchors布局

anchors布局 import QtQuick 2.2 import QtQuick.Window 2.2 Window {visible: truewidth: 640height: 480title: qsTr("Hello World")Rectangle {width: 300;height: 200;color: "blue";Rectangle {id: rectl;anchors.left: parent.left;anchors.leftMar…

PDU Session Anchors

在同一时刻,UE可以建立多条到同一个DN或不同DN的PDU会话;建立到同一个DN的多个PDU会话时,通过不同的UPF;每条PDU会话对应的SMF可以不同。 为了支持到DN的可选择路由功能或支持SSC mode 3模式,SMF可以控制PDU session的…

kmeans++聚类生成anchors

kmeans聚类生成anchors 说明 使用yolo系列通常需要通过kmeans聚类算法生成anchors, 但kmeans算法本身具有一定的局限性,聚类结果容易受初始值选取影响。 因此通过改进原kmeans_for_anchors.py实现 kmeans聚类生成anchors。具体实现如下: i…

anchors如何获得_Yolov3通过k-means聚类得到自己数据的anchors

本文代码参考: https://github.com/lars76/kmeans-anchor-boxes Yolov3中默认的9个anchors是作者通过对voc数据聚类得到的。 anchors 10,14, 23,27, 37,58, 81,82, 135,169, 344,319 不过,当我们训练自己的数据时,如果也采用默认的anchors,可…

Unity 锚点 Anchors

锚点介绍 锚点是Rect Transform组件中的属性,用于描述当前物体相对于父物体的对齐方式。 选中一个UI元素,就会显示其父物体的矩形框以及相对于父物体的锚点。 锚点只能位于父物体的矩形框之内。 锚点表现为四个相对出现的小三角形, 它们时而…

QML anchors 锚布局

锚布局 锚布局有7种锚线 anchors.leftanchors.rightanchors.topanchors.bottomanchors.horizontalCenteranchors.verticalCenteranchors.baseline 5种锚边距 anchors.leftMarginanchors.rightMarginanchors.topMarginanchors.bottomMarginanchors.margins 3种锚偏移 anchor…

anchors生成

关于修改anchor anchor与图片的输入分辨率有关系。 You should use this repository to get anchors: https://github.com/AlexeyAB/darknet By using this command for Yolo v3 (or v2): ./darknet detector calc_anchors data/hand.data -num_of_clusters 9 -width 720 -he…

使用k-means聚类anchors

在之前讲yolo理论基础知识时有提到过,从yolov2开始使用的anchors都是通过聚类得到的。如果想了解更多yolo相关的知识可以看看我在bilibili上录得视频:https://www.bilibili.com/video/BV1yi4y1g7ro 今天补下之前没有细讲的聚类anchors相关知识&#xff…

细说物体检测中的Anchors

点击上方“AI公园”,关注公众号,选择加“星标“或“置顶” 作者:Raghul Asokan 编译:ronghuaiyang 导读 给大家再次解释一下Anchors在物体检测中的作用。 今天,我将讨论在物体检测器中引入的一个优雅的概念 —— Ancho…

带图讲解,深度学习YOLO里面的anchors的进阶理解

如果有了解过yolo网络,那肯定也听说过anchors,当然anchors这个概念布置在YOLO里面才有,在其他的目标检测中也存在anchors这个概念。对于anchors计算的一些公式这篇文章就不进行讲解了,这篇文章主要是讲在训练网络模型过程中anchor…

Linux终端的网易云音乐——musicbox

网易云音乐是听歌的不错的选择,如果能够在命令行听歌就更cool了。特来推荐musicbox。 网易云音乐的musicbox是网易云音乐命令行版本,这款命令行的客户端使用 Python 构建,以 mpg123 作为播放后端。提供了很多使用的功能,如&#x…