我想创建一个可重用的组件,我可以在其中传递一个模型,即
["red", "green", "blue", "black", "orange", "pink", "gray", "navy", "magenta"]
它会填充Grid
模型数据的矩形。如果模型中有超过 6 个项目,它将填充其他“页面”。
目前我使用StackLayout
有 2Grid
件物品,Repeater
在它们内部,我将模型分为 2 件:
model: ["red", "green", "blue", "black", "orange", "pink"]
model: ["gray", "navy", "magenta"]
用矩形填充每个“页面”。
编写逻辑以将模型动态分离为每个页面的单独部分似乎过于复杂。我已经尝试过GridView
,但我找不到重要的属性,例如Grid
:
topPadding: 10
bottomPadding: 10
leftPadding: 20
rightPadding: 20
spacing: 10
columns: 2
我的例子的来源:
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
}
}