QML 布局详解
创始人
2025-06-01 15:03:59
0

1.简介

在QML中布局分为以下几种:绝对坐标、锚布局、定位器、布局管理器。

2.绝对坐标

设置x、y、width、height能够定位一个组件的位置,这种方法扩展性太差。

    Button{width: 50height: 50x: 10y:10background: Rectangle{color:"red"}}

3.锚布局

示例:实现一行按钮。

Window {id: windowobjectName: "window"visible: truewidth: 400height: 500title: qsTr("Hello World")//做一行按钮Button{id:b1width: 50height: 50anchors.centerIn: parent    //在Window的中心background: Rectangle{color:"red"}}Button{id:b2width: 50height: 50anchors.left: b1.right      //b2放在b1的右边,左边距20anchors.top: b1.topanchors.leftMargin: 20background: Rectangle{color:"black"}}Button{id:b3width: 50height: 50anchors.right: b1.left      //b3放在b1的左边,右边距20anchors.top: b1.topanchors.rightMargin: 20background: Rectangle{color:"blue"}}Button{id:b4width: 25                //b4放在b1的中心height: 25anchors.horizontalCenter: b1.horizontalCenteranchors.verticalCenter: b1.verticalCenterbackground: Rectangle{color:"yellow"}}}

3.定位器

Row和Column差不多

Window {id: windowobjectName: "window"visible: truewidth: 400height: 500title: qsTr("Hello World")Rectangle{border.color: "black"border.width: 2width: 200height: 300Row {Repeater {model: 3Button {width: 30height: 40background: Rectangle{color:"yellow"}text:index}}spacing: 10 //设置间隔padding: 10 //设置内边距}}
}

Flow:流式布局

示例:Flow项自动将子Text项并排放置,当超出父控件边界时会自动换行。

Window {id: windowobjectName: "window"visible: truewidth: 400height: 500title: qsTr("Hello World")Rectangle{border.color: "black"border.width: 2width: 200height: 300Flow {anchors.fill: parentanchors.margins: 4spacing: 10Text { text: "Text"; font.pixelSize: 40 }Text { text: "items"; font.pixelSize: 40 }Text { text: "flowing"; font.pixelSize: 40 }Text { text: "inside"; font.pixelSize: 40 }Text { text: "a"; font.pixelSize: 40 }Text { text: "Flow"; font.pixelSize: 40 }Text { text: "item"; font.pixelSize: 40 }}}
}

Grid:网格布局

可以控制绘制几行几列。

Window {id: windowobjectName: "window"visible: truewidth: 400height: 500title: qsTr("Hello World")Rectangle{border.color: "black"border.width: 2width: 200height: 300Grid {columns: 3spacing: 2padding: 10Rectangle { color: "red"; width: 50; height: 50 }Rectangle { color: "green"; width: 20; height: 50 }Rectangle { color: "blue"; width: 50; height: 20 }Rectangle { color: "cyan"; width: 50; height: 50 }Rectangle { color: "magenta"; width: 10; height: 10 }}}
}

4.布局管理器

有很多附加属性。

  • Layout.minimumWidth:最小宽度
  • Layout.minimumHeight:最小高度
  • Layout.preferredWidth:首选宽度
  • Layout.preferredHeight:首选高度
  • Layout.maximumWidth:最大宽度
  • Layout.maximumHeight:最大高度
  • Layout.fillWidth:如果为true,尽可能变宽
  • Layout.fillHeight:如果为true,尽可能变高
  • Layout.alignment:对齐方式
  • Layout.margins:外边距
  • Layout.leftMargin:左外边距
  • Layout.rightMargin:右外边距
  • Layout.topMargin:上外边距
  • Layout.bottomMargin:下外边距

Window {id: windowobjectName: "window"visible: truewidth: 400height: 500title: qsTr("Hello World")RowLayout {id: layoutanchors.fill: parentspacing: 6Rectangle {color: 'teal'Layout.fillWidth: trueLayout.minimumWidth: 50Layout.preferredWidth: 100Layout.maximumWidth: 300Layout.minimumHeight: 150Text {anchors.centerIn: parenttext: parent.width + 'x' + parent.height}}Rectangle {color: 'plum'Layout.fillWidth: trueLayout.minimumWidth: 100Layout.preferredWidth: 200Layout.preferredHeight: 100Text {anchors.centerIn: parenttext: parent.width + 'x' + parent.height}}}
}

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
【Ctfer训练计划】——(三... 作者名:Demo不是emo  主页面链接:主页传送门 创作初心ÿ...