Potentially related: https://stackoverflow.com/a/30860285/3363018
I've been trying to create a QML layout with items that span variable numbers of rows and columns. So, e.g., a rectangle that spans two rows and four columns, one to the right of it that spans one row and two columns, and one underneath that spans three rows and five columns. A generic attempt at creating this is below:
import QtQuick 2.5
import QtQuick.Layouts 1.2
import QtQuick.Controls 1.4
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
GridLayout {
anchors.fill: parent
columns: 5
rows: 2
Rectangle {
Layout.column: 0
Layout.columnSpan: 4
Layout.row: 0
Layout.rowSpan: 2
Layout.fillHeight: true
Layout.fillWidth: true
color: "red"
}
Rectangle {
Layout.column: 4
Layout.columnSpan: 1
Layout.row: 0
Layout.rowSpan: 2
Layout.fillHeight: true
Layout.fillWidth: true
color: "green"
}
Rectangle {
Layout.column: 0
Layout.columnSpan: 5
Layout.row: 2
Layout.rowSpan: 3
Layout.fillHeight: true
Layout.fillWidth: true
color: "blue"
}
}
}
which results in this:
As you can see, Layout.rowSpan and Layout.columnspan don't seem to be working. Instead, the top two rows seem to take up 1:1 rather than 4:1 and the top part vs the bottom part are 1:1 rather than 2:3. I suspect this is related to Layout.fillHeight and Layout.fillWidth. The documentation states:
If this property is true, the item will be as wide as possible while
respecting the given constraints.
but I'm not sure what "the given constraints" are in this context. I had hoped it would include the row and column span but it seems not.
I could probably directly calculate the item widths and heights (or maybe Layout.preferredWidth and Layout.preferredHeight) but that would seem to make Layout.rowSpan and Layout.columnSpan completely superfluous. Is there a way to automatically calculate the heights based on grid rows and columns?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…