I want to create a reusable component where I could pass a model i.e.
["red", "green", "blue", "black", "orange", "pink", "gray", "navy", "magenta"]
And it would fill Grid
with rectangles of model data. And if there are more than let's say 6 items in model it would then fill other "page".
That's how it should look like:
Currently I use StackLayout
have 2 Grid
items and Repeater
inside of them and I divided my model into 2:
model: ["red", "green", "blue", "black", "orange", "pink"]
model: ["gray", "navy", "magenta"]
To fill each "page" with rectangles.
Writing logic to dynamically separate model into separate parts for each page seems overly complicated.
I have tried GridView
but I couldn't find important properties like in Grid
:
topPadding: 10
bottomPadding: 10
leftPadding: 20
rightPadding: 20
spacing: 10
columns: 2
Source of my example:
import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Rectangle {
id: mainArea
width: 400
height: 400
color: "beige"
StackLayout {
id: stackLayout
anchors.fill: parent
currentIndex: 0
Grid {
anchors.fill: parent
topPadding: 10
bottomPadding: 10
leftPadding: 20
rightPadding: 20
spacing: 10
columns: 2
property int maxRows: 3
Repeater {
model: ["red", "green", "blue", "black", "orange", "pink"]
Rectangle {
width: (parent.width - parent.leftPadding - parent.rightPadding - parent.spacing) / parent.columns
height: (parent.height - parent.topPadding - parent.bottomPadding - (parent.maxRows - 1) * parent.spacing) / parent.maxRows
color: modelData
}
}
}
Grid {
anchors.fill: parent
topPadding: 10
bottomPadding: 10
leftPadding: 20
rightPadding: 20
spacing: 10
columns: 2
property int maxRows: 3
Repeater {
model: ["gray", "navy", "magenta"]
Rectangle {
width: (parent.width - parent.leftPadding - parent.rightPadding - parent.spacing) / parent.columns
height: (parent.height - parent.topPadding - parent.bottomPadding - (parent.maxRows - 1) * parent.spacing) / parent.maxRows
color: modelData
}
}
}
}
}
Button {
anchors.bottom: mainArea.verticalCenter
anchors.bottomMargin: 5
anchors.left: mainArea.right
text: "<"
onClicked: stackLayout.currentIndex = 0
}
Button {
anchors.top: mainArea.verticalCenter
anchors.topMargin: 5
anchors.left: mainArea.right
text: ">"
onClicked: stackLayout.currentIndex = 1
}
}