QML深入学习四(布局用法)

锚点anchors

复制代码
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Layouts 1.15

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Qml 91 learn")
    Rectangle{
        id:containerId
        width: 300
        height: width
        border.color: "black"
        anchors.centerIn: parent

        Rectangle{
            id:topLeftid
            width: 100
            height: 100
            color: "magenta"
            Text {
                id: topLefttxtid
                text: qsTr("1")
                anchors.centerIn: parent
                font.pointSize: 20
            }
        }

        Rectangle{
            anchors.left: topLeftid.right
            id:topMidid
            width: 100
            height: 100
            color: "yellowgreen"
            Text {
                id: topMidtxtid
                text: qsTr("2")
                anchors.centerIn: parent
                font.pointSize: 20
            }
        }
        Rectangle{
            anchors.left: topMidid.right
            id:topRightid
            width: 100
            height: 100
            color: "dodgerblue"
            Text {
                id: topRighttxtid
                text: qsTr("3")
                anchors.centerIn: parent
                font.pointSize: 20
            }
        }

        Rectangle{
            anchors.top: topLeftid.bottom
            id:midLeftId
            width: 100
            height: 100
            color: "red"
            Text {
                id: midLeftTxtId
                text: qsTr("4")
                anchors.centerIn: parent
                font.pixelSize: 20
            }
        }
        //方式一:锚点anchors
        // Rectangle{
        //     anchors.left: midLeftId.right
        //     anchors.top:topRightid.bottom
        //     id:midmidId
        //     width: 100
        //     height: 100
        //     color: "green"
        //     Text {
        //         id: midmidTxtId
        //         text: qsTr("5")
        //         anchors.centerIn: parent
        //         font.pixelSize: 20
        //     }
        // }
        //方式二:使用中心
        Rectangle{
            anchors.centerIn: parent
            id:midmidId
            width: 100
            height: 100
            color: "green"
            Text {
                id: midmidTxtId
                text: qsTr("5")
                anchors.centerIn: parent
                font.pixelSize: 20
            }
        }
        Rectangle{
            anchors.left: midmidId.right
            anchors.top:topRightid.bottom
            id:midRightId
            width: 100
            height: 100
            color: "blue"
            Text {
                id: midRightTxtId
                text: qsTr("6")
                anchors.centerIn: parent
                font.pixelSize: 20
            }
        }
        Rectangle{
            anchors.top:midmidId.bottom
            id:bottomLeftId
            width: 100
            height: 100
            color: "royalblue"
            Text {
                id: bottomLeftTxtId
                text: qsTr("7")
                anchors.centerIn: parent
                font.pixelSize: 20
            }
        }
        Rectangle{
            anchors.left: bottomLeftId.right
            anchors.top:midmidId.bottom
            id:bottomMidId
            width: 100
            height: 100
            color: "yellow"
            Text {
                id: bottomMidTxtId
                text: qsTr("8")
                anchors.centerIn: parent
                font.pixelSize: 20
            }
        }

        Rectangle{
            anchors.left: bottomMidId.right
            anchors.top:midmidId.bottom
            id:bottomRightId
            width: 100
            height: 100
            color: "pink"
            Text {
                id:bottomRightTxtId
                text: qsTr("9")
                anchors.centerIn: parent
                font.pixelSize: 20
            }
        }
    }
    //Qt5的windows没有focus属性,用以下实现
    Shortcut {
        sequence: "q"
        context: Qt.ApplicationShortcut
        onActivated: Qt.quit()
    }

}

锚点边距与偏移

复制代码
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Layouts 1.15

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Qml 91 learn")
    Rectangle{
        id:containerId
        width: 300
        height: width
        border.color: "black"
        anchors.centerIn: parent

        Rectangle{
            id:topLeftid
            width: 100
            height: 100
            color: "magenta"
            Text {
                id: topLefttxtid
                text: qsTr("1")
                anchors.centerIn: parent
                font.pointSize: 20
            }
        }

        Rectangle{
            anchors.left: topLeftid.right
            id:topMidid
            width: 100
            height: 100
            color: "yellowgreen"
            Text {
                id: topMidtxtid
                text: qsTr("2")
                anchors.centerIn: parent
                font.pointSize: 20
            }
        }
        Rectangle{
            anchors.left: topMidid.right
            id:topRightid
            width: 100
            height: 100
            color: "dodgerblue"
            Text {
                id: topRighttxtid
                text: qsTr("3")
                anchors.centerIn: parent
                font.pointSize: 20
            }
        }

        Rectangle{
            anchors.top: topLeftid.bottom
            id:midLeftId
            width: 100
            height: 100
            color: "red"
            Text {
                id: midLeftTxtId
                text: qsTr("4")
                anchors.centerIn: parent
                font.pixelSize: 20
            }
        }
        //方式一:锚点anchors
        Rectangle{
            // anchors.left: midLeftId.right
            // anchors.top:topRightid.bottom
        // anchors.topMargin: 10 //会影响其他元素。。。所有的
        // anchors.rightMargin: 10 //设置了锚点设置边距才有用。。。

            //方式二:使用中心
            // anchors.centerIn: parent
            //等价于:
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.verticalCenter: parent.verticalCenter

            anchors.horizontalCenterOffset: 10
            anchors.verticalCenterOffset: -10

            id:midmidId
            width: 100
            height: 100
            color: "green"
            Text {
                id: midmidTxtId
                text: qsTr("5")
                anchors.centerIn: parent
                font.pixelSize: 20
            }
        }
        // //方式二:使用中心
        // Rectangle{
        //     anchors.centerIn: parent
        //     id:midmidId
        //     width: 100
        //     height: 100
        //     color: "green"
        //     Text {
        //         id: midmidTxtId
        //         text: qsTr("5")
        //         anchors.centerIn: parent
        //         font.pixelSize: 20
        //     }
        // }
        Rectangle{
            anchors.left: midmidId.right
            anchors.top:topRightid.bottom
            id:midRightId
            width: 100
            height: 100
            color: "blue"
            Text {
                id: midRightTxtId
                text: qsTr("6")
                anchors.centerIn: parent
                font.pixelSize: 20
            }
        }
        Rectangle{
            // anchors.top:midmidId.bottom
            anchors.top:midLeftId.bottom

            id:bottomLeftId
            width: 100
            height: 100
            color: "royalblue"
            Text {
                id: bottomLeftTxtId
                text: qsTr("7")
                anchors.centerIn: parent
                font.pixelSize: 20
            }
        }
        Rectangle{
            anchors.left: bottomLeftId.right
            anchors.top:midmidId.bottom
            id:bottomMidId
            width: 100
            height: 100
            color: "yellow"
            Text {
                id: bottomMidTxtId
                text: qsTr("8")
                anchors.centerIn: parent
                font.pixelSize: 20
            }
        }

        Rectangle{
            anchors.left: bottomMidId.right
            anchors.top:midmidId.bottom
            id:bottomRightId
            width: 100
            height: 100
            color: "pink"
            Text {
                id:bottomRightTxtId
                text: qsTr("9")
                anchors.centerIn: parent
                font.pixelSize: 20
            }
        }
    }
    //Qt5的windows没有focus属性,用以下实现
    Shortcut {
        sequence: "q"
        context: Qt.ApplicationShortcut
        onActivated: Qt.quit()
    }

}

锚点父级与兄弟关系

复制代码
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Layouts 1.15

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Qml 91 learn")
    Rectangle{
        id:containerId
        width: 300
        height: width
        border.color: "black"
        anchors.centerIn: parent

        Rectangle{
            id:topLeftid
            anchors.top: siblingId.bottom //no
            //qrc:/main.qml:17:9: QML Rectangle: Cannot anchor to an item that isn't a parent or sibling.


            width: 100
            height: width
            color: "magenta"
        }
    }
    Rectangle{
        id:siblingId
        width: 100
        height: width
        color: "black"
        // anchors.right: containerId.left //只能containerId,不能topLeftid

    }
    //Qt5的windows没有focus属性,用以下实现
    Shortcut {
        sequence: "q"
        context: Qt.ApplicationShortcut
        onActivated: Qt.quit()
    }

}

定位器(Positioners)

Grid网格布局

默认是四列,需要指定rows或者columns

复制代码
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Layouts 1.15

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Qml 91 learn")
    //网格布局,需要划分为多少列,多少行
    Grid{
        columns: 2
        // rows: 2
        Rectangle{
            width: 20
            height: 100
            color: "magenta"
            Text {
                text: qsTr("1")
                anchors.centerIn: parent
            }
        }
        Rectangle{
            width: 60
            height: 60
            color: "green"
            Text {
                text: qsTr("2")
                anchors.centerIn: parent
            }
        }
        Rectangle{
            width: 60
            height: 60
            color: "blue"
            Text {
                text: qsTr("3")
                anchors.centerIn: parent
            }
        }
        Rectangle{
            width: 60
            height: 60
            color: "red"
            Text {
                text: qsTr("4")
                anchors.centerIn: parent
            }
        }
    }
    //Qt5的windows没有focus属性,用以下实现
    Shortcut {
        sequence: "q"
        context: Qt.ApplicationShortcut
        onActivated: Qt.quit()
    }

}

Row行布局

显示效果为一行

Column列布局

显示效果为一列

设置布局的位置

此时,我想改变第一个矩形的位置,应该怎么做呢?

复制代码
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Layouts 1.15

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Qml 91 learn")
    //网格布局,需要划分为多少列,多少行
    Grid{
        horizontalItemAlignment: Grid.AlignTop |Grid.AlignRight
        verticalItemAlignment: Grid.AlignVCenter //中间对齐
        LayoutMirroring.enabled: true //镜像效果,反转
        LayoutMirroring.childrenInherit: true //里面的子元素也继承
        rows: 2
        Rectangle{
            width: 20
            height: 100
            color: "magenta"
            Text {
                text: qsTr("1")
                anchors.centerIn: parent
            }
        }
        Rectangle{
            width: 60
            height: 60
            color: "green"
            Text {
                text: qsTr("2")
                anchors.centerIn: parent
            }
        }
        Rectangle{
            width: 20
            height: 50
            color: "blue"
            Text {
                text: qsTr("3")
                anchors.centerIn: parent
            }
        }
        Rectangle{
            width: 60
            height: 60
            color: "red"
            Text {
                text: qsTr("4")
                anchors.centerIn: parent
            }
        }
    }
    //Qt5的windows没有focus属性,用以下实现
    Shortcut {
        sequence: "q"
        context: Qt.ApplicationShortcut
        onActivated: Qt.quit()
    }

}

Layouts布局

GridLayout布局

默认不局限与4列,他的作用是可以随着窗口大小改变的同时,自动改变大小。

复制代码
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Layouts 1.15

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Qml 91 learn")
    //网格布局
    GridLayout{
        anchors.fill: parent
        columns: 3
        Rectangle{
            width: 20
            height: 100
            color: "magenta"
            Text {
                text: qsTr("1")
                anchors.centerIn: parent
            }
        }
        Rectangle{
            width: 60
            height: 60
            color: "green"
            Text {
                text: qsTr("2")
                anchors.centerIn: parent
            }
        }
        Rectangle{
            width: 20
            height: 50
            color: "blue"
            Text {
                text: qsTr("3")
                anchors.centerIn: parent
            }
        }
        Rectangle{
            width: 60
            height: 60
            color: "red"
            Text {
                text: qsTr("4")
                anchors.centerIn: parent
            }
        }
        Rectangle{
            width: 60
            height: 60
            color: "yellow"
            Text {
                text: qsTr("5")
                anchors.centerIn: parent
            }
        }
    }
    //Qt5的windows没有focus属性,用以下实现
    Shortcut {
        sequence: "q"
        context: Qt.ApplicationShortcut
        onActivated: Qt.quit()
    }

}

RowLayout

ColumnLayout

这两个也是一样的,就不着重介绍了,以下代码介绍设置单个的位置。Grid就实现不了

复制代码
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Layouts 1.15

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Qml 91 learn")
    //网格布局
    GridLayout{
        columns: 2

        anchors.fill: parent
        Rectangle{
            width: 20
            height: 100
            color: "magenta"
            Text {
                text: qsTr("1")
                anchors.centerIn: parent
            }
            Layout.alignment: Qt.AlignRight |Qt.AlignTop
        }
        Rectangle{
            width: 60
            height: 60
            color: "green"
            Text {
                text: qsTr("2")
                anchors.centerIn: parent
            }
        }
        Rectangle{
            width: 20
            height: 50
            color: "blue"
            Text {
                text: qsTr("3")
                anchors.centerIn: parent
            }
        }
        Rectangle{
            width: 60
            height: 60
            color: "red"
            Text {
                text: qsTr("4")
                anchors.centerIn: parent
            }
        }
        Rectangle{
            width: 60
            height: 60
            color: "yellow"
            Text {
                text: qsTr("5")
                anchors.centerIn: parent
            }
        }
    }
    //Qt5的windows没有focus属性,用以下实现
    Shortcut {
        sequence: "q"
        context: Qt.ApplicationShortcut
        onActivated: Qt.quit()
    }

}

设置多行跨越

复制代码
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Layouts 1.15

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Qml 91 learn")
    //网格布局
    GridLayout{
        columns: 2
        Layout.fillHeight: true //不能设置全部的,必须单个去设置
        Layout.fillWidth: true //不能设置全部的,必须单个去设置
        anchors.fill: parent

        Rectangle{
            width: 20
            height: 100
            color: "magenta"
            Text {
                text: qsTr("1")
                anchors.centerIn: parent
            }
            Layout.alignment: Qt.AlignRight |Qt.AlignTop //设置位置
            Layout.fillWidth: true //填满可用空间
            Layout.fillHeight: true //填满可用空间
            Layout.maximumHeight: 150 //设置最大高度
            Layout.maximumWidth: 140 //设置最大宽度

        }
        Rectangle{
            width: 60
            height: 60
            color: "green"
            Text {
                text: qsTr("2")
                anchors.centerIn: parent
            }
            Layout.fillHeight: true
            Layout.fillWidth: true
        }
        Rectangle{
            width: 20
            height: 50
            color: "blue"
            Text {
                text: qsTr("3")
                anchors.centerIn: parent
            }
            Layout.fillHeight: true
            Layout.fillWidth: true
        }
        Rectangle{
            width: 60
            height: 60
            color: "red"
            Text {
                text: qsTr("4")
                anchors.centerIn: parent
            }
            Layout.fillHeight: true
            Layout.fillWidth: true
        }
        Rectangle{
            width: 60
            height: 60
            color: "yellow"
            Text {
                text: qsTr("5")
                anchors.centerIn: parent
            }
            Layout.fillHeight: true
            Layout.fillWidth: true
            Layout.columnSpan: 2 //设置占据的列数
        }
    }
    //Qt5的windows没有focus属性,用以下实现
    Shortcut {
        sequence: "q"
        context: Qt.ApplicationShortcut
        onActivated: Qt.quit()
    }

}

设置方向

layoutDirection: Qt.RightToLeft //设置方向

Flow元素

Flow项将其子项定位为页面上的单词,包裹它们以创建行或列的项目。

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

        Text { 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 }
    }

设置之后,矩阵可以根据窗口大小,进行换行操作

复制代码
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Layouts 1.15

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Qml 91 learn")


    Flow{
        Layout.fillHeight: true //不能设置全部的,必须单个去设置
        Layout.fillWidth: true //不能设置全部的,必须单个去设置
        anchors.fill: parent
        Rectangle{
            width: 20
            height: 100
            color: "magenta"
            Text {
                text: qsTr("1")
                anchors.centerIn: parent
            }
            Layout.alignment: Qt.AlignRight |Qt.AlignTop //设置位置
            Layout.fillWidth: true //填满可用空间
            Layout.fillHeight: true //填满可用空间
            Layout.maximumHeight: 150 //设置最大高度
            Layout.maximumWidth: 140 //设置最大宽度

        }
        Rectangle{
            width: 60
            height: 60
            color: "green"
            Text {
                text: qsTr("2")
                anchors.centerIn: parent
            }
            Layout.fillHeight: true
            Layout.fillWidth: true
        }
        Rectangle{
            width: 20
            height: 50
            color: "blue"
            Text {
                text: qsTr("3")
                anchors.centerIn: parent
            }
            Layout.fillHeight: true
            Layout.fillWidth: true
        }
        Rectangle{
            width: 60
            height: 60
            color: "red"
            Text {
                text: qsTr("4")
                anchors.centerIn: parent
            }
            Layout.fillHeight: true
            Layout.fillWidth: true
        }
        Rectangle{
            width: 60
            height: 60
            color: "yellow"
            Text {
                text: qsTr("5")
                anchors.centerIn: parent
            }
            Layout.fillHeight: true
            Layout.fillWidth: true
            Layout.columnSpan: 2 //设置占据的列数
        }
    }

    //Qt5的windows没有focus属性,用以下实现
    Shortcut {
        sequence: "q"
        context: Qt.ApplicationShortcut
        onActivated: Qt.quit()
    }

}

flow: Flow.TopToBottom//排列顺序

layoutDirection: Qt.RightToLeft //位置

相关推荐
承渊政道2 小时前
【Python编程—从入门到实践】(用户账户:给Django网站装上登录系统,让每个人拥有自己的学习笔记)
python·学习·pycharm·django
诗句藏于尽头3 小时前
游戏《完美神话》-使用Ai-agent进行修复已停服游戏到本地成功部署以及GM端网页完整教程
人工智能·学习·游戏
希和20273 小时前
51 单片机学习笔记:LCD1602、独立按键、I2C 与 AT24C02 掉电存储(江科大课程小白笔记)
笔记·单片机·学习
承渊政道3 小时前
【Python编程—从入门到实践】(设置样式并部署:给Django网站换新装,把学习笔记发布到互联网)
python·学习·pycharm·django
~kiss~15 小时前
A Survey of On-Policy Distillation for LLMs 学习(一)
学习
小杨小杨、努力变强!16 小时前
python学习笔记
笔记·学习
Star(up up up)1 天前
Redis Windows 安装教程:使用Windows 的 Redis 压缩包
redis·笔记·学习·程序人生·云原生·学习方法·改行学it
乌暮1 天前
Spring AOP 与 Spring 事务:用大白话彻底理解原理与实践
java·数据库·spring boot·后端·sql·学习·spring
蒸蒸yyyyzwd1 天前
从邓明学习的后端笔记
笔记·学习
机器学习之心1 天前
基于变量相关图与残差学习的 GCN 多变量回归方法——混凝土 28 天抗压强度预测
学习·回归·gcn