QML 布局篇

学习目标:Qml 布局

学习内容

1、锚(anchors)布局,锚布局是一种相对布局,

2、常用布局方式 :Row(行布局)、Column(列布局)、Grid(网格布局)和 Flow(流方布局)。

项目效果

常用布局

瞄点布局

核心代码

常用布局

import QtQuick 2.12
import QtQuick.Window 2.12

Window {
    visible: true
    width: 610
    height: 480
    title: qsTr("Hello World")

    Rectangle{
        id:myrow
        x:30;y:0;
        width: 200;
        height: 200;
        border.color: "red"
        border.width: 20
        Text {
            id: rowtext
            text: qsTr("Row布局")
            x:10;y:10
        }
        Row{
            anchors.centerIn: parent
            spacing: 20
            Rectangle{color:"green";width: 10;height: 10 }
            Rectangle{color:"red";width: 10;height: 10 }
            Rectangle{color:"blue";width: 10;height: 10 }
            Rectangle{color:"cyan";width: 10;height: 10 }
            Rectangle{color:"orange";width: 10;height: 10 }
            Rectangle{color:"black";width: 10;height: 10 }
        }
    }


    Rectangle{
        id:mycolum
        anchors.top: myrow.bottom
        width: 200
        height: 200
        border.width: 4
        border.color: "green"
        // radius:数字
        Text {
            id: coloumtext
            text: qsTr("Column布局")
            x:10;y:10
        }
        Column{
            anchors.centerIn: parent // 相对背景父窗口居中
            spacing: 10 // 设置行与行之间的间距
            Rectangle{color:"green";width: 10;height: 10 }
            Rectangle{color:"red";width: 10;height: 10 }
            Rectangle{color:"blue";width: 10;height: 10 }
            Rectangle{color:"cyan";width: 10;height: 10 }
            Rectangle{color:"orange";width: 10;height: 10 }
            Rectangle{color:"black";width: 10;height: 10 }
        }
    }


    Rectangle{
        id:mygrid
        anchors.left: myrow.right
        width: 200
        height: 200
        border.width: 4
        border.color: "blue"

        Text {
            id: gridtext
            text: qsTr("网格布局")
            x:10;y:10
        }
        Grid{
            anchors.centerIn: parent // 相对背景父窗口居中
            spacing: 20 // 设置行与行之间的间距
            columns: 5
            Rectangle{color:"green";width: 10;height: 10 }
            Rectangle{color:"red";width: 10;height: 10 }
            Rectangle{color:"blue";width: 10;height: 10 }
            Rectangle{color:"cyan";width: 10;height: 10 }
            Rectangle{color:"orange";width: 10;height: 10 }
            Rectangle{color:"black";width: 10;height: 10 }
            Rectangle{color:"green";width: 10;height: 10 }
            Rectangle{color:"red";width: 10;height: 10 }
            Rectangle{color:"blue";width: 10;height: 10 }
            Rectangle{color:"cyan";width: 10;height: 10 }
            Rectangle{color:"orange";width: 10;height: 10 }
            Rectangle{color:"black";width: 10;height: 10 }
            Rectangle{color:"green";width: 10;height: 10 }
            Rectangle{color:"red";width: 10;height: 10 }
            Rectangle{color:"blue";width: 10;height: 10 }
            Rectangle{color:"cyan";width: 10;height: 10 }
            Rectangle{color:"orange";width: 10;height: 10 }
            Rectangle{color:"black";width: 10;height: 10 }
            Rectangle{color:"green";width: 10;height: 10 }
            Rectangle{color:"red";width: 10;height: 10 }
            Rectangle{color:"blue";width: 10;height: 10 }
            Rectangle{color:"cyan";width: 10;height: 10 }
            Rectangle{color:"orange";width: 10;height: 10 }
            Rectangle{color:"black";width: 10;height: 10 }
            Rectangle{color:"green";width: 10;height: 10 }
        }
    }


    Rectangle{
        id:myflow
        width: 200
        height: 200
        border.color: "red"
        border.width: 4

        anchors.top:mygrid.bottom
        anchors.left: mygrid.left

        Text {
            id: flowtext
            text: qsTr("flow布局")
            x:10;y:10
        }
        //流水线
        Flow{
                //  填充方式
                anchors.fill:parent
                anchors.margins: 5
                spacing: 20 // 设置行与行之间的间距

                Rectangle{color:"green";width: 10;height: 10 }
                Rectangle{color:"red";width: 10;height: 10 }
                Rectangle{color:"blue";width: 10;height: 10 }
                Rectangle{color:"cyan";width: 10;height: 10 }
                Rectangle{color:"orange";width: 10;height: 10 }
                Rectangle{color:"black";width: 10;height: 10 }
                Rectangle{color:"green";width: 10;height: 10 }
                Rectangle{color:"red";width: 10;height: 10 }
                Rectangle{color:"blue";width: 10;height: 10 }
                Rectangle{color:"cyan";width: 10;height: 10 }
                Rectangle{color:"orange";width: 10;height: 10 }
                Rectangle{color:"black";width: 10;height: 10 }
                Rectangle{color:"green";width: 10;height: 10 }
                Rectangle{color:"red";width: 10;height: 10 }
                Rectangle{color:"blue";width: 10;height: 10 }
                Rectangle{color:"cyan";width: 10;height: 10 }
                Rectangle{color:"orange";width: 10;height: 10 }
                Rectangle{color:"black";width: 10;height: 10 }
                Rectangle{color:"green";width: 10;height: 10 }
                Rectangle{color:"red";width: 10;height: 10 }
                Rectangle{color:"blue";width: 10;height: 10 }
                Rectangle{color:"cyan";width: 10;height: 10 }
                Rectangle{color:"orange";width: 10;height: 10 }
                Rectangle{color:"black";width: 10;height: 10 }
                Rectangle{color:"green";width: 10;height: 10 }
            }
    }

}

瞄点布局

import QtQuick 2.12
import QtQuick.Window 2.12

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")


    Text {
        id: title
        text: qsTr("我是测试文本")
        x:100;y:100;


        anchors.topMargin: 300
        color: "blue";
        font.pixelSize: 25;
    }

    Rectangle{
        id :rect
        x:50;y:100
        anchors.horizontalCenter: parent.horizontalCenter;
        anchors.verticalCenter: parent.verticalCenter

        width: 200;
        height: 200;

        color: "cyan"
        border.width: 20
        border.color: "red"

        radius: 10;

        TextInput{
            id:rect_text;
            width: 50;
            height: 50;

            anchors.top:parent.top
            anchors.topMargin: 30
            anchors.left: parent.left
            anchors.leftMargin: 30

            color: "blue"
            //字体
            font.pixelSize: 22
            focus: true;

        }


    }


}

 最后附上源代码链接
对您有帮助的话,帮忙点个star

Qt demo: 学习qt过程 (gitee.com)

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值