我正在尝试在 Flickable 中放置大量(5k - 10k 个元素)的矩形。数据来自自定义模型,QAbstractListModel 的子类。预期结果如下所示:
我的第一种方法是使用带有委托的中继器来创建这样的矩形:
Repeater {
id: repeater
model: particleListModel
delegate: mapDelegate
}
Component {
id: mapDelegate
Rectangle {
property bool checked: false
id: particle
color: "transparent"
width: model.boundswidth
height: model.boundsheight
x: model.boundsx
y: model.boundsy
MouseArea {
id: mouseArea
anchors.fill: parent
hoverEnabled: true
onClicked: {
console.log(model.index)
particleIndex = model.index
}
}
border.color: mouseArea.containsMouse ? "lightgreen" : (model.index === particleIndex) ? "green" : "red"
border.width: 1.5
}
}
这可以很好地显示矩形,但是加载需要很长时间(约 6 秒)并在加载时阻塞 UI。因此,我的第二种方法是使用 Loader 异步填充地图,如下所示:
Component {
id: mapDelegate
Loader {
id: particle
asynchronous: true
onLoaded: {
console.log("loaded " + model.index)
}
sourceComponent:
Rectangle {
property bool checked: false
color: "red"
width: model.boundswidth
height: model.boundsheight
x: model.boundsx
y: model.boundsy
MouseArea {
id: mouseArea
anchors.fill: parent
hoverEnabled: true
onClicked: {
console.log(model.index)
particleIndex = model.index
}
}
border.color: mouseArea.containsMouse ? "lightgreen" : (model.index === particleIndex) ? "green" : "red"
border.width: 1.5
}
}
}
虽然这有效并且不会阻塞 UI,但完成所有元素的加载需要很长时间(约 25 秒)。前几个元素加载得非常快,但到最后加载变得越来越缓慢,速度减慢到可以看到添加单个元素的程度。
我想在不阻塞 UI 的情况下提高在合理时间内加载所有元素的性能。还有另一种我想念的方式吗?或者有没有一种优雅的方式来只加载当前视图中需要的元素?在这里采取什么正确方法?