【发布时间】:2023-04-08 21:12:01
【问题描述】:
有谁知道,当我在其头部添加一些元素时,如何防止向下滚动 QML ListView?我想以类似 Twitter 的方式更新 ListView,当它始终保持其位置并且只能显式滑动时。
【问题讨论】:
有谁知道,当我在其头部添加一些元素时,如何防止向下滚动 QML ListView?我想以类似 Twitter 的方式更新 ListView,当它始终保持其位置并且只能显式滑动时。
【问题讨论】:
实际上insert 函数完成了这项工作。您可以将其插入顶部或任何所需位置,例如modelName.insert(0,{object});。一个工作示例是here。
import QtQuick 1.0
Rectangle {
id: rectangle1
width: 320
height: 480
ListModel {
id: cModel
ListElement {
name: "Bill Smith"
number: "555 3264"
}
ListElement {
name: "John Brown"
number: "555 8426"
}
ListElement {
name: "Sam Wise"
number: "555 0473"
}
}
ListView {
id: list_view1
width: rectangle1.width
height: rectangle1.height - 40
anchors.horizontalCenter: parent.horizontalCenter
delegate: Text {
text: name + ": " + number
}
model: cModel
}
Rectangle {
id: rectangle2
width: 320
height: 40
color: "#ffffff"
anchors.top: list_view1.bottom
Text {
id: text1
text: qsTr("Click to add!")
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
font.pixelSize: 16
MouseArea {
id: mouse_area1
anchors.fill: parent
onClicked: addNewItemTop();
}
}
}
function addNewItemTop()
{ var i = Math.random();
cModel.insert(0,{"name" : "New Number", "number":i.toString()});
}
}
【讨论】: