Qt QML-如何调整GridView的项目?
我想将所有矩形放入Qt QML-如何调整GridView的项目?,qt,gridview,qml,Qt,Gridview,Qml,我想将所有矩形放入GridView。我没有使用GridLayout,因为当我从GridLayout中删除一个矩形时,它们再次与GridLayout对齐 所以我使用了GridView和我的代码: import QtQuick 2.10 import QtQuick.Window 2.10 import QtQuick.Layouts 1.3 Window { visible: true width: 420 height: 720 title: qsTr("Hello World
GridView
。我没有使用GridLayout
,因为当我从GridLayout
中删除一个矩形时,它们再次与GridLayout
对齐所以我使用了
GridView
和我的代码:
import QtQuick 2.10
import QtQuick.Window 2.10
import QtQuick.Layouts 1.3
Window {
visible: true
width: 420
height: 720
title: qsTr("Hello World")
GridView {
id: area
// If device orientation is vertical, GridView shall be 4x(model/4)
property int verticalChoice: (parent.width < parent.height) ? 4 : 10
//If device orientation is horizontal, GridView shall be (model/(model/4))x4
property int horizontalChoice: (parent.width > parent.height) ? 10 : 4
width: cellWidth * verticalChoice
height: cellHeight * horizontalChoice
anchors.centerIn: parent
cellWidth: (parent.width / verticalChoice)
cellHeight: (parent.height / horizontalChoice)
model: 40
clip: true
interactive: false
delegate: Rectangle {
id: rect
width: area.cellWidth - 5
height: area.cellHeight - 5
color: 'red'
Text {
text: index
color: 'white'
anchors.centerIn: parent
}
MouseArea {
anchors.fill: parent
onClicked: rect.destroy(1000)
}
}
}
}
导入QtQuick 2.10
导入QtQuick.Window 2.10
导入QtQuick.Layouts 1.3
窗口{
可见:正确
宽度:420
身高:720
标题:qsTr(“你好世界”)
网格视图{
id:区域
//如果设备方向垂直,GridView应为4x(型号/4)
属性int verticalChoice:(parent.widthparent.height)?10:4
宽度:单元格宽度*垂直选择
高度:单元高度*水平选择
anchors.centerIn:父对象
单元格宽度:(parent.width/verticalChoice)
单元高度:(parent.height/horizontalChoice)
型号:40
剪辑:对
交互:错误
代表:矩形{
id:rect
宽度:area.cellWidth-5
高度:area.cellHeight-5
颜色:“红色”
正文{
文本:索引
颜色:“白色”
anchors.centerIn:父对象
}
鼠耳{
锚定。填充:父级
点击一次:直接销毁(1000)
}
}
}
}
我将GridView的model
设置为40
,并将interactive
设置为false
。但我只看到了15个这样的项目:
我还编写了anchors.centerIn:parent
,但它没有运行。如何进行调整(以适应和对齐)?对于项目数量,您只需在分配水平选择时出错,将网格设置为4*4,而不是10*4:
property int verticalChoice: (parent.width < parent.height) ? 4 : 10
property int horizontalChoice: (parent.width < parent.height) ? 10 : 4
哦,谢谢,这是真的。那么,为什么anchors.centerIn:parent
没有运行?我编辑了我的代码,就像你的代码一样:gist.github.com/anonymous/9f219711985f8c6931ece5a7a02ab0f,但它不起作用。
delegate: Item {
width: area.cellWidth
height: area.cellHeight
Rectangle {
color: 'red'
anchors {
fill: parent
margins: 5
}
Text {
text: index
color: 'white'
anchors.centerIn: parent
}
}
}