QT QML布局
一、基础布局方式
-
锚点布局(Anchors)
通过定义元素与其他元素或父容器的锚点关系实现定位,支持动态调整和边距控制。Rectangle { anchors.left: parent.left // 左对齐父容器 anchors.top: parent.top // 顶部对齐父容器 anchors.margins: 10 // 统一设置四周边距 width: 100; height: 50 }
- 关键属性:
anchors.left
、anchors.right
、anchors.fill
(填充父容器)、anchors.centerIn
(居中)。 - 边距控制:
anchors.margins
(统一边距)或单独设置anchors.leftMargin
等。
- 关键属性:
-
定位器布局(Positioners)
使用内置定位器(如Row
、Column
、Grid
、Flow
)自动排列子元素。- 水平布局(Row):
Row { spacing: 5 // 子元素间距 Rectangle { width: 50; height: 50; c
- 水平布局(Row):