翻译|使用教程|编辑:龚雪|2021-12-27 10:53:51.903|阅读 319 次
概述:本文主要介绍如何在QML中的定位器和布局,欢迎下载框架产品体验~
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
相关链接:
有很多种方法可以在QML中定位项目,以下是简要的概述。
通过设置项目的 x,y 属性,可以将项目放置在屏幕上的特定 x,y 坐标处。 根据可视化坐标系规则,这将设置它们相对于父级左上角的位置。
结合使用绑定替代这些属性的常量值,通过将 x 和 y 坐标设置为适当的绑定,也可以轻松实现相对定位。
import QtQuick Item { width: 100; height: 100 Rectangle { // Manually positioned at 20,20 x: 20 y: 20 width: 80 height: 80 color: "red" } }
Item类型提供了锚定到其他Item类型的功能,每个项目有7条锚线:左、右、垂直居中、顶部、底部、基线和水平居中。三个垂直锚线可以锚定到另一个项目的三个垂直锚线中的任何一个,四个水平锚线可以锚定到另一个项目的水平锚线。
import QtQuick Item { width: 200; height: 200 Rectangle { // Anchored to 20px off the top right corner of the parent anchors.right: parent.right anchors.top: parent.top anchors.margins: 20 // Sets all margins at once width: 80 height: 80 color: "orange" } Rectangle { // Anchored to 20px off the top center corner of the parent. // Notice the different group property syntax for 'anchors' compared to // the previous Rectangle. Both are valid. anchors { horizontalCenter: parent.horizontalCenter; top: parent.top; topMargin: 20 } width: 80 height: 80 color: "green" } }
对于想要以常规模式定位一组类型的常见情况,Qt Quick提供了一些定位器类型。放置在定位器中的物品会以某种方式自动定位; 例如, Row将项目定位为水平相邻(形成一行)。
import QtQuick Item { width: 300; height: 100 Row { // The "Row" type lays out its child items in a horizontal line spacing: 20 // Places 20px of space between items Rectangle { width: 80; height: 80; color: "red" } Rectangle { width: 80; height: 80; color: "green" } Rectangle { width: 80; height: 80; color: "blue" } } }
Layout types功能与定位器类似,但允许进一步细化或限制布局。 具体来说,Layout types允许您:
GroupBox { id: gridBox title: "Grid layout" Layout.fillWidth: true GridLayout { id: gridLayout rows: 3 flow: GridLayout.TopToBottom anchors.fill: parent Label { text: "Line 1" } Label { text: "Line 2" } Label { text: "Line 3" } TextField { } TextField { } TextField { } TextArea { text: "This widget spans over three rows in the GridLayout.\n" + "All items in the GridLayout are implicitly positioned from top to bottom." Layout.rowSpan: 3 Layout.fillHeight: true Layout.fillWidth: true } } }
上面的代码片段来自基本布局示例,该代码段显示了在布局中添加各种字段和项目的简单性。 GridLayout 可以调整大小,并且可以通过各种属性自定义其格式。
Qt技术交流群:166830288 欢迎一起进群讨论
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com
文章转载自:慧都网