你为什么要计算
blockHeight
blockWidth
ColumnLayout
?
使用
Layout.fillWidth
和
Layout.fillHeight
属性来表示
列布局
列布局
它将完成你试图自行编程的计算。
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.3
Window {
id: wndId
property int wndWidth: 200
property int wndHeight: 300
visible: true
width: wndWidth
height: wndHeight
title: qsTr("Testing ColumnLayout")
Rectangle {
id: rectId
property int borderWidth: 5
property int blockCount: 4
property int blocksSpace: 8
width: wndId.wndWidth
height: wndId.wndHeight
border.color: "blue"
border.width: borderWidth
Component.onCompleted: print("Outter Rectangle w=" + rectId.width + " h=" + rectId.height)
ColumnLayout {
spacing: rectId.blocksSpace
Layout.alignment: Qt.AlignBottom
anchors.fill: parent
anchors.margins: rectId.borderWidth * 2
Repeater {
id: repId
model: rectId.blockCount
Rectangle {
id: blockId
color: "brown"
Layout.fillWidth: true
Layout.fillHeight: true
Component.onCompleted: {
print("Inner Rectangle", index)
print(" blockCount=" + rectId.blockCount);
print(" blockId.width=" + blockId.width + " blockId.height=" + blockId.height)
print(" blockWidth=" + rectId.blockWidth + " blockHeight=" + rectId.blockHeight)
}
Component.onDestruction: print("~Inner Rectangle")
}
Component.onCompleted: print("Repeater")
Component.onDestruction: print("~Repeater")
}
}
MouseArea {
anchors.fill: parent
onClicked: {
print("Mouse clicked!");
rectId.blockCount = 2;
print("blockHeight= " + rectId.blockHeight);
}
}
}
}
为了保存
方块高度
如果你坚持保持计算,因为在现实世界中它更难(足够公平),我建议使用
implicitHeight
和
implicitWidth
。这是因为布局引擎不会触发宽度/高度的更改,因为它应该自己设置这些值,但它会监视隐式大小:
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.3
Window {
id: wndId
property int wndWidth: 200
property int wndHeight: 300
visible: true
width: wndWidth
height: wndHeight
title: qsTr("Testing ColumnLayout")
Rectangle {
id: rectId
property int borderWidth: 5
property int blockCount: 4
property int blocksSpace: 8
width: wndId.wndWidth
height: wndId.wndHeight
border.color: "blue"
border.width: borderWidth
property int blockWidth: rectId.width - (rectId.borderWidth * 4)
property int blockHeight: updateBlockHeight()
function updateBlockHeight(numBlocks)
{
var newHeight = (rectId.height - ((rectId.borderWidth + rectId.blocksSpace)*2) - (rectId.blocksSpace * (rectId.blockCount-1))) / rectId.blockCount;
print("updateBlockHeight: newHeight=", newHeight);
return newHeight;
}
Component.onCompleted: print("Outter Rectangle w=" + rectId.width + " h=" + rectId.height)
ColumnLayout {
spacing: rectId.blocksSpace
Layout.alignment: Qt.AlignBottom
anchors.fill: parent
anchors.margins: rectId.borderWidth * 2
Repeater {
id: repId
model: rectId.blockCount
Rectangle {
id: blockId
color: "brown"
implicitWidth: rectId.blockWidth
implicitHeight: rectId.blockHeight
onXChanged: print("x[",index,"]=", x)
onYChanged: print("y[",index,"]=", y)
Component.onCompleted: {
print("Inner Rectangle", index)
print(" blockCount=" + rectId.blockCount);
print(" blockId.width=" + blockId.width + " blockId.height=" + blockId.height)
print(" blockWidth=" + rectId.blockWidth + " blockHeight=" + rectId.blockHeight)
}
Component.onDestruction: print("~Inner Rectangle")
}
Component.onCompleted: print("Repeater")
Component.onDestruction: print("~Repeater")
}
}
MouseArea {
anchors.fill: parent
onClicked: {
print("Mouse clicked!");
rectId.blockCount = 2;
print("blockHeight= " + rectId.blockHeight);
}
}
}
}
updateBlockHeight