所以我有一個ListView和ListModel,我正在動態地添加物件。我希望當一個新的物件被添加時發生一個displaced影片,但是從我所看到的,只有add過渡被觸發,而displaced過渡從未被觸發。根據教程,這樣做是可行的:
Rectangle {
anchors.fill: parent
串列視圖 {
width: 240; height: 320; height: 320.
模型。ListModel {
id: model
}
組件 {
id: del
行 {
高度。20。
間距。20
Text { text: name }
文本 { 文本: 年齡 }
}
}
委托:del
添加。Transition {
NumberAnimation { property: "opacity"; from: 0; to: 1.0; duration: 1000 }
}
流離失所。Transition {
NumberAnimation { 屬性。"x,y"; duration: 1000; easing.type: Easing.OutBounce }
}
焦點。true true
Keys.onSpacePressed: model.insert(0, { "name"/span>: "Item" model.count, "age":21})
}
}
但這只觸發了add過渡。
Rectangle {
高度。500
寬度: 0.90 * parent.width
錨點 {
top: parent
topMargin: 30 30
左邊:parent.left
leftMargin: 45
}
串列視圖 {
anchors.fill: parent
模型:notificationModel
委托:notificationDelegate
間隔。30
添加。Transition {
NumberAnimation { 屬性。"opacity"; from: 0; to: 1; duration: 1000 }
}
流離失所。Transition {
NumberAnimation { 屬性。"x,y"; duration: 1000; easing.type: Easing.OutBounce }
}
}
}
ListModel {
id: notificationModel
Component.onCompleted: {
notificationModel.append({"name"/span>: "Tony"})
}
}
計時器 {
時間間隔。5000; running: true; repeat: true; running: true
onTriggered: notificationModel.append({"name"/span>: "Stark"})
}
組件 {
id: notificationDelegate
行 {
間隔。20
Text { text: name; color: "white" }
}
}
知道為什么會發生這種情況嗎?謝謝!
uj5u.com熱心網友回復:
displaced過渡是在串列中的一個專案因為另一個專案被添加/移除/等等而被迫移動時觸發的。這個例子的作業原理是,一個專案被插入到串列的開頭,導致其余的專案被替換。
在您展示的當前代碼中,問題在于您使用了append函式而不是insert。Append 將專案添加到串列的底部,所以沒有專案會被替換。
在你編輯你的問題之前,你有一個不同的問題。您正在使用 insert 將專案添加到串列的開頭,但是每次您添加一個專案時,您都會清除整個串列并重新建立它。因此,同樣的,沒有任何東西被替換,因為整個串列每次都會被重新創建。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/310181.html
標籤:
上一篇:Maven中的SpringBoot:"在META-INF/spring.factories中沒有發現自動配置類"
