Qml学习——布局
创始人
2024-05-24 05:08:48
0

最近在学习Qml,但对Qml的各种用法都不太熟悉,总是会搞忘,所以写几篇文章对学习过程中的遇到的东西做一个记录。
学习参考视频:https://www.bilibili.com/video/BV1Ay4y1W7xd?p=1&vd_source=0b527ff208c63f0b1150450fd7023fd8

目录

  • 1 锚点布局
    • 1.1 锚定线和间距
    • 1.2 示例
      • 控件之间相对位置
      • 设置间隙
      • 填满窗口
      • 混合使用
  • 2 Qt Quick Layouts布局
    • 2.1 GridLayout(网格布局)
    • 2.2 ColumnLayout(列布局)
    • 2.3 RowLayout(行布局)
    • 2.3 StackLayout(栈布局)


1 锚点布局

Qt Quick控件的基类Item提供了anchor(锚点),可以用来布局控件的位置。
下面是一个简单的锚点布局示例,将矩形块布局到窗口的右下角。

Window {visible: true; width: 200; height: 200Rectangle {width: 50; height: 50; color: "blue";anchors.right: parent.right      //矩形的右锚定线对齐窗口的右锚定线anchors.bottom: parent.bottom	 //矩形的下锚定线对齐窗口的下锚定线}
}

在这里插入图片描述
在这个示例中,矩形的右锚定线和下锚定线分别被设置为了窗口的右锚定线和下锚定线,所以最终矩形出现在窗口的右下角。

anchors提供了以下属性。

属性名类型描述
anchors.alignWhenCenteredbool居中时是否对齐
anchors.baselineAnchorLine基准线,作用和anchors.top一样
anchors.baselineOffsetreal基准线偏移量
anchors.bottomAnchorLine下锚定线
anchors.bottomMarginreal下锚定线间距
anchors.centerInItem位于目标控件中心
anchors.fillItem填满目标控件
anchors.horizontalCenterAnchorLine水平方向的中心锚定线
anchors.horizontalCenterOffsetreal水平方向的中心锚定线偏移量
anchors.leftAnchorLine左锚定线
anchors.leftMarginreal左锚定线间距
anchors.marginsreal上下左右四根锚定线的间距
anchors.rightAnchorLine右锚定线
anchors.rightMarginreal右锚定线间距
anchors.topAnchorLine上锚定线
anchors.topMarginreal上锚定线间距
anchors.verticalCenterAnchorLine垂直方向的中心锚定线
anchors.verticalCenterOffsetreal垂直方向的中心锚定线偏移量

1.1 锚定线和间距

以anchors.top为例。
若没有设置anchors.topMargin或anchors.topMargin为0。
在这里插入图片描述
若设置了anchors.topMargin不为0。
在这里插入图片描述

1.2 示例

控件之间相对位置

Window {visible: true; width: 200; height: 200Rectangle {id: r1width: 50; height: 50; color: "red";/* 以r2为基准布局 */anchors.right: r2.leftanchors.top: r2.top}Rectangle {id: r2width: 50; height: 50; color: "blue";/* 布局到窗口中央 */anchors.centerIn: parent}
}

在这里插入图片描述

设置间隙

Window {visible: true; width: 200; height: 200Rectangle {width: 50; height: 50; color: "blue";/* 设置矩形位置在parent的右下角,间隙为10 */anchors.right: parent.rightanchors.rightMargin: 10anchors.bottom: parent.bottomanchors.bottomMargin: 10}
}

在这里插入图片描述

填满窗口

Window {visible: true; width: 200; height: 200Rectangle {color: "blue";anchors.fill: parent}
}

在这里插入图片描述

混合使用

Window {visible: true; width: 200; height: 200Rectangle {id: r1color: "blue";anchors.fill: parentanchors.topMargin: 50anchors.rightMargin: 50}Rectangle {id: r2color: "red";anchors.top: parent.topanchors.left: parent.leftanchors.right: parent.rightanchors.bottom: r1.top}Rectangle {id: r3color: "green";anchors.top: r1.topanchors.left: r1.rightanchors.right: parent.rightanchors.bottom: parent.verticalCenter}Rectangle {id: r4color: "orange";anchors.top: parent.verticalCenteranchors.left: r1.rightanchors.right: parent.rightanchors.bottom: parent.bottom}
}

请添加图片描述

2 Qt Quick Layouts布局

Qt Quick Layouts提供了以下几种布局方式。

名称描述
GridLayout提供一种在网格中动态排列项目的方法
ColumnLayout与GridLayout相同,但只有一列
RowLayout与GridLayout相同,但只有一行
StackLayout只会同时显示一个控件的控件堆栈

2.1 GridLayout(网格布局)

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.12Window {visible: true; width: 200; height: 120GridLayout {id: gridcolumns: 2Text { text: "1";}Text { text: "22";}Text { text: "333";}Text { text: "4444";}Text { text: "55555";}}
}

在这里插入图片描述

2.2 ColumnLayout(列布局)

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.12Window {visible: true; width: 200; height: 120ColumnLayout {Text { text: "1";}Text { text: "22";}Text { text: "333";}Text { text: "4444";}Text { text: "55555";}}
}

在这里插入图片描述

2.3 RowLayout(行布局)

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.12Window {visible: true; width: 200; height: 120RowLayout {Text { text: "1";}Text { text: "22";}Text { text: "333";}Text { text: "4444";}Text { text: "55555";}}
}

在这里插入图片描述

2.3 StackLayout(栈布局)

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.12Window {visible: true; width: 200; height: 120StackLayout {id: stackText { text: "1";}Text { text: "22";}Text { text: "333";}Text { text: "4444";}Text { text: "55555";}}RowLayout {anchors.bottom: parent.bottomButton {text: '上一个'onClicked: stack.currentIndex > 0 ? stack.currentIndex-- : 0}Button {text: '下一个'onClicked: stack.currentIndex < 4 ? stack.currentIndex++ : 0}}
}

请添加图片描述

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
ChatGPT 怎么用最新详细... ChatGPT 以其强大的信息整合和对话能力惊艳了全球,在自然语言处理上面表现出了惊人...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...