Rowlayout,qml布局里的一种布局器,它类似于qwidget里的QHBoxLayout,可以把它想象成一个横着摆放的矩形的箱子,添加进去的控件就横着塞进去,类似下图
在qwidget里所有控件会被均匀的撑满父容器,但是在qml里是需要设置一些属性了,主要是
Layout.fillWidth
和Layout.fillHeight
下面我们用代码来说明
先看效果图
代码
import QtQuick 2.13
import QtQuick.Window 2.13
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3
ApplicationWindow{
id: mainwindow
visible: false
width: 600
height: 600
flags: Qt.FramelessWindowHint
title: qsTr("Hello World")
color: "BLACK"
RowLayout{
spacing:5
anchors.fill: parent
Rectangle{
color:"red"
Layout.fillWidth: parent.width
Layout.fillHeight: parent.height
}
Rectangle{
color:"red"
Layout.fillWidth: parent.width
Layout.fillHeight: parent.height
}Rectangle{
color:"red"
Layout.fillWidth: parent.width
Layout.fillHeight: parent.height
}Rectangle{
color:"red"
Layout.fillWidth: parent.width
Layout.fillHeight: parent.height
}Rectangle{
color:"red"
Layout.fillWidth: parent.width
Layout.fillHeight: parent.height
}
}
}
我试着又添加了一些矩形
可以看到采用上述代码设置后 它就是遵循均匀的分布在父容器这个规则的,当然它还有其他策略,后续如果我会用到,会继续写出来。