QML学习笔记(三十一)QML的Flow定位器

前言

本节将介绍一个名叫Flow的定位器,如含义所言,这种布局就像会流动一样,会根据组件数量、尺寸、父容器的大小自动适应布局。

一、代码演示

因为比较简单,我们直接看代码:

cpp 复制代码
Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("QmlFlow")

    Flow{
        anchors.fill: parent
        anchors.margins: 4
        spacing: 10

        Rectangle{
            id: topLeftRectId
            width: 100
            height: width
            color: "magenta"
            Text {
                anchors.centerIn: parent
                text: "1"
                font.pointSize: 20
            }
        }

        Rectangle{
            id: topCenterRectId
            width: 100
            height: width
            color: "yellowgreen"
            Text {
                anchors.centerIn: parent
                text: "2"
                font.pointSize: 20
            }
        }

        Rectangle{
            id: topRightRectId
            width: 100
            height: width
            color: "dodgerblue"
            Text {
                anchors.centerIn: parent
                text: "3"
                font.pointSize: 20
            }
        }

        Rectangle{
            id: centerLeftRectId
            width: 100
            height: width
            color: "red"
            Text {
                anchors.centerIn: parent
                text: "4"
                font.pointSize: 20
            }
        }

        Rectangle{
            id: centerCenterRectId
            width: 100
            height: width
            color: "green"
            Text {
                anchors.centerIn: parent
                text: "5"
                font.pointSize: 20
            }
        }

        Rectangle{
            id: centerRightRectId

            width: 100
            height: width
            color: "blue"
            Text {
                anchors.centerIn: parent
                text: "6"
                font.pointSize: 20
            }
        }

        Rectangle{
            id: bottomLeftRectId
            width: 100
            height: width
            color: "royalblue"
            Text {
                anchors.centerIn: parent
                text: "7"
                font.pointSize: 20
            }
        }

        Rectangle{
            id: bottomCenterRectId
            width: 100
            height: width
            color: "yellow"
            Text {
                anchors.centerIn: parent
                text: "8"
                font.pointSize: 20
            }
        }

        Rectangle{
            id: bottomRightRectId
            width: 100
            height: width
            color: "pink"
            Text {
                anchors.centerIn: parent
                text: "9"
                font.pointSize: 20
            }
        }
    }
}

代码很长,但也只是九宫格矩形而已。

我们聚焦在这几句:

cpp 复制代码
    Flow{
        anchors.fill: parent
        anchors.margins: 4
        spacing: 10
//...}

它的使用非常简单,甚至不需要设置什么复杂的属性,这里只简单给了间距。

当然,Flow生效的前提是每一个组件都已经写了固定尺寸。

我们运行代码看看:

拖拽一下窗口:

自动换行的效果非常丝滑,且在某种场合当中非常好用。

比如我们需要自己手搓一个文件管理器,需要在一个窗口内部实现多文件的排布,此时就可以用这种方式,就比较灵活了。

另外就是还有一个换行方向的问题,我们可以指定:

cpp 复制代码
flow:Flow.TopToBottom

效果就会变成这样:


如果再加上:

cpp 复制代码
layoutDirection: Qt.RightToLeft

二、总结

Flow的定位方式虽然没那么常用,但在某种场合下可能是一种更优的选择。至此,我们应该已经掌握了比较丰富的定位和布局方式了。

相关推荐
菜的不敢吱声1 天前
swift学习第4天
服务器·学习·swift
李慕婉学姐1 天前
【开题答辩过程】以《基于JAVA的校园即时配送系统的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·开发语言·数据库
web小白成长日记1 天前
企业级 Vue3 + Element Plus 主题定制架构:从“能用”到“好用”的进阶之路
前端·架构
孙严Pay1 天前
快捷支付:高效安全的在线支付新选择
笔记·科技·计算机网络·其他·微信
じ☆冷颜〃1 天前
黎曼几何驱动的算法与系统设计:理论、实践与跨领域应用
笔记·python·深度学习·网络协议·算法·机器学习
想进部的张同学1 天前
hilinux-3599---设备学习---以及部署yolo
学习·yolo·海思
APIshop1 天前
Python 爬虫获取 item_get_web —— 淘宝商品 SKU、详情图、券后价全流程解析
前端·爬虫·python
风送雨1 天前
FastMCP 2.0 服务端开发教学文档(下)
服务器·前端·网络·人工智能·python·ai
XTTX1101 天前
Vue3+Cesium教程(36)--动态设置降雨效果
前端·javascript·vue.js
HyperAI超神经1 天前
【vLLM 学习】Rlhf
人工智能·深度学习·学习·机器学习·vllm