QT之布局篇

QT提供很多种布局的方式,非常灵活,下面介绍的是Qt quick提供几种布局。
分别是:

  • anchors ,锚布局
  • Row ,行布局
  • Column ,列布局
  • Grid ,表格布局
  • Flow ,流式布局

anchors

锚布局是使用Item元素提供的属性anchors来实现的,它是一种相对布局,相对于其他元素的位置来确定自己的位置,基准线有水平中心(horizontalCenter)、垂直中心(verticalCenter)、左(left)、右(right)、上(top)、下(bottom),还有一条基线(baseline)主要用于文本布局的。

通过这些线以及偏移我们就可以很方便的去进行相对布局了,一般使用比较多的就是居中anchors.centerIn = parent,以及充满整个父区域anchors.fill = parent,其它具体可以参考帮助文档

Row

import QtQuick 2.7
import QtQuick.Controls 2.0

Rectangle {
    width: 640
    height: 480
    color: "#DCDCDC"

    Row {
        padding: 10
        spacing: 20

        Rectangle {
            width: 100
            height: 100
            color: "#FF0000"
        }

        Rectangle {
            width: 100
            height: 100
            color: "#00FF00"
        }

        Rectangle {
            width: 100
            height: 100
            color: "#0000FF"
        }
    }
}

效果图:
row

Colomn

import QtQuick 2.7
import QtQuick.Controls 2.0

Rectangle {
    width: 640
    height: 480
    color: "#DCDCDC"

    Column {
        padding: 10
        spacing: 20

        Rectangle {
            width: 100
            height: 100
            color: "#FF0000"
        }

        Rectangle {
            width: 100
            height: 100
            color: "#00FF00"
        }

        Rectangle {
            width: 100
            height: 100
            color: "#0000FF"
        }
    }
}

效果图:
column

Grid

import QtQuick 2.7
import QtQuick.Controls 2.0

Rectangle {
    width: 640
    height: 480
    color: "#DCDCDC"

    Grid {
        padding: 10
        spacing: 20

        rows: 2
        columns: 2

        Rectangle {
            width: 100
            height: 100
            color: "#FF0000"
        }

        Rectangle {
            width: 100
            height: 100
            color: "#00FF00"
        }

        Rectangle {
            width: 100
            height: 100
            color: "#0000FF"
        }
    }
}

效果图:
grid

表格布局中可以通过属性rows和columns指定几行几列,然后对下面的元素进行排列

Flow

import QtQuick 2.7
import QtQuick.Controls 2.0

Rectangle {
    width: 300
    height: 300
    color: "#DCDCDC"

    Flow {
        padding: 10
        spacing: 20

        flow: Flow.LeftToRight
        anchors.fill: parent

        Rectangle {
            width: 100
            height: 100
            color: "#FF0000"
        }

        Rectangle {
            width: 100
            height: 100
            color: "#00FF00"
        }

        Rectangle {
            width: 100
            height: 100
            color: "#0000FF"
        }
    }
}

效果图:
flow

Flow可以通过flow属性指定布局的方向,默认是Flow.LeftToRight,常用的还有Flow.TopToBottom,当在那个方向无法排列显示更多元素时,它就会折行或折列。

当然还有更多的布局方法,这些方法也可以嵌套使用来完成更加复杂的布局。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ithewei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值