Differently from previous positioners, such as Column
or Row
, layouts were introduced to support graphical scaling of UI, also by filling available space (in this specific case fill their parent). In that sense the spacing
property should not be seen as a strict upper bound to the spacing between Item
s but as the minimum allowed distance between them.
The current approach to solve your issue is to use a "filler" Item
, which uses the fillHeight
property. This Item
occupies all the space left by the other Item
s inside the layout thus packing them together, as needed:
import QtQuick 2.5
import QtQuick.Window 2.2
Window {
visible: true
width: 100
height: 200
ColumnLayout {
anchors.fill: parent
spacing: 2
Rectangle {
height: 50
width: 50
color: "red"
}
Rectangle {
height: 50
width: 50
color: "green"
}
Rectangle {
height: 50
width: 50
color: "blue"
}
Item { Layout.fillHeight: true } // <-- filler here
}
}
Note that you can exploit the same approach and add a filler at the beginning of the layout to vertically center the children Item
s. Finally, note that in this case it would be advisable to use a Column
which lays down the Item
s correctly as expected, disregarding the available space left.
Just make your choice.
It should be noted that while this approach works Layout
s provide a lot of properties to control the size of the Item
s. Please refer to the other answer for some insight on the subject.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…