Another very similar use case is to have the initial, smaller flow horizontally centred as well:
import QtQuick 2.0
import QtQuick.Controls 2.0
ApplicationWindow {
id: window
width: 600
height: 600
visible: true
Slider {
id: slider
value: 10
to: 100
stepSize: 1
width: parent.width
anchors.bottom: parent.bottom
}
Flow {
id: flow
width: Math.min(implicitW, maxW)
spacing: 4
anchors.horizontalCenter: parent.horizontalCenter
readonly property int columnImplicitWidth: children[0].implicitWidth + spacing
readonly property int implicitW: Math.max(0, (repeater.count * columnImplicitWidth) - spacing)
readonly property int maxW: Math.floor(parent.width / columnImplicitWidth) * columnImplicitWidth
Repeater {
id: repeater
model: slider.value
delegate: Rectangle {
implicitWidth: 40
implicitHeight: 60
color: "transparent"
border.color: "darkorange"
}
}
}
Rectangle {
anchors.fill: flow
color: "transparent"
border.color: "red"
}
}
I think GridLayout
is even better for this, though, because unlike Flow
, it doesn't leave the extra space on the right edge:
import QtQuick 2.0
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
ApplicationWindow {
id: window
width: 600
height: 600
visible: true
Slider {
id: slider
value: 10
to: 100
stepSize: 1
width: parent.width
anchors.bottom: parent.bottom
}
GridLayout {
columns: implicitW < parent.width ? -1 : parent.width / columnImplicitWidth
rowSpacing: 4
columnSpacing: 4
anchors.horizontalCenter: parent.horizontalCenter
property int columnImplicitWidth: children[0].implicitWidth + columnSpacing
property int implicitW: repeater.count * columnImplicitWidth
Repeater {
id: repeater
model: slider.value
delegate: Rectangle {
implicitWidth: 40
implicitHeight: 60
color: "transparent"
border.color: "darkorange"
}
}
}
}
This is how it looks with GridLayout
:
