Qt 矩形不采用其父高度/宽度
它是一个bug还是一个特性Qt 矩形不采用其父高度/宽度,qt,qml,qtquick2,Qt,Qml,Qtquick2,它是一个bug还是一个特性 import QtQuick 2.5 import QtQuick.Controls 1.4 import QtQuick.Layouts 1.2 ApplicationWindow { id: root height: 600 width: 800 ColumnLayout { id: rootLayout anchors.fill: parent // width: parent.w
import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.2
ApplicationWindow {
id: root
height: 600
width: 800
ColumnLayout {
id: rootLayout
anchors.fill: parent
// width: parent.width
// height: parent.height
Rectangle {
color: "red"
width: parent.width
height: 200
}
Rectangle {
color: "green"
width: parent.width
height: 200
}
Rectangle {
color: "blue"
width: parent.width
height: 200
}
}
}
这段代码似乎不能满足我的需要。它应该打开一个窗口,在一列中绘制三个矩形width
等于父width
和height
等于200
在每个矩形中我将宽度和高度属性更改为常量值(200
,300
,等等),但我希望使其相对于父布局
当我输入Rectangle
项width:root.width
时,它也可以工作。那么,为什么像在我的代码中那样键入时它不工作呢?使用Layout.fillWidth:true
:
import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.2
ApplicationWindow {
id: root
height: 600
width: 800
visible: true
ColumnLayout {
id: rootLayout
anchors.fill: parent
Rectangle {
color: "red"
Layout.fillWidth: true
height: 200
}
Rectangle {
color: "green"
Layout.fillWidth: true
height: 200
}
Rectangle {
color: "blue"
Layout.fillWidth: true
height: 200
}
}
}
说明其用法:
布局类型的对象附着到布局的子对象,以提供有关该项的特定于布局的信息。附着对象的特性会影响布局如何排列项目
例如,如果默认值不符合要求,您可以指定minimumWidth、preferredWidth和maximumWidth。
调整布局大小时,项目可能会增长或收缩。因此,项目具有最小尺寸、首选尺寸和最大尺寸
如果未在项目上明确指定最小尺寸,则尺寸设置为0。如果未在项目上明确指定最大尺寸,则尺寸设置为Number.POSITIVE_∞
对于布局,隐式最小和最大大小取决于布局的内容
fillWidth和fillHeight属性可以为true或false。如果为false,则项目的大小将固定为其首选大小。否则,随着布局的调整,它将在其最小和最大大小之间增长或收缩
重要的一点是:
注意:不建议对版面中项目的x、y、width或height属性进行绑定,因为这会与版面的目标冲突,并会导致绑定循环。
您可能应该遵循此建议,并替换
height: 200
与
因为你的矩形都是一样大小的。在这种情况下,所有矩形都将争夺可用高度,布局将为它们提供相等的高度百分比
因此,有几种方法可以调整版面中项目的大小,但是fillWidth
是为您的用例设计的。别担心,这不是一个bug。ColumnLayout
(RowLayout
和GridLayout
中的项目)必须使用组件的附加属性调整大小。它们允许我们通过设置项目对齐、最小/最大尺寸或是否应使用整个宽度
/高度
来创建复杂结构。以您的代码为基础,查看以下代码:
import QtQuick 2.5
import QtQuick.Controls 1.1
import QtQuick.Layouts 1.1
ApplicationWindow {
id: root
height: 600
width: 800
ColumnLayout {
id: rootLayout
anchors.fill: parent
Rectangle {
color: "red"
Layout.fillWidth: true
height: 150
}
Rectangle {
color: "green"
Layout.preferredWidth: 400
Layout.alignment: Qt.AlignHCenter
height: 100
}
Rectangle {
color: "blue"
Layout.maximumWidth: 300
Layout.preferredWidth: 400 //actual width will be 300(=maximumWidth)
Layout.fillHeight: true
}
}
}
将width
或height
设置为常量似乎也能起作用,但我会使用为这些组件建议的API
我希望这能对您有所帮助。我感谢您的编辑,巴卡罗佐,但我认为您在这方面做得有点过火——并非所有内容都需要格式化。在这种情况下,我只是指一般意义上的矩形普威尔,我一直很感激你的回答@Mitch(以一种明显的方式,这种方式不能直接在评论中陈述;D)但是,你知道,这里是一个炎热的夏天我很抱歉。下次我再检查三次!
import QtQuick 2.5
import QtQuick.Controls 1.1
import QtQuick.Layouts 1.1
ApplicationWindow {
id: root
height: 600
width: 800
ColumnLayout {
id: rootLayout
anchors.fill: parent
Rectangle {
color: "red"
Layout.fillWidth: true
height: 150
}
Rectangle {
color: "green"
Layout.preferredWidth: 400
Layout.alignment: Qt.AlignHCenter
height: 100
}
Rectangle {
color: "blue"
Layout.maximumWidth: 300
Layout.preferredWidth: 400 //actual width will be 300(=maximumWidth)
Layout.fillHeight: true
}
}
}