实际上,功能就是这一职务。 您可在以下级别或任何理想的职位上插入:modelName.insert(0,{object});
。 这里有一个工作例子。
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()});
}
}