此代碼將做到這一點。基本上你需要創建一個虛擬委託項目,然後創建另一個具有相同寬度,高度,x和y值的項目。將內部項目的父項設置爲您的網格視圖。以下是修改後的代碼,可以很好地進行動畫製作。
import QtQuick 1.0
Rectangle {
id: mainRect
width: 300; height: 400
ListModel {
id: appModel
ListElement { modelcolor: "#FF0000"}
ListElement { modelcolor: "#00FF00"}
ListElement { modelcolor: "#0000FF"}
}
Component {
id: appDelegate
Item {
id: main
width: grid.cellWidth; height: grid.cellHeight
Rectangle {
id: item; parent: grid
x: main.x; y: main.y
width: main.width; height: main.height;
color: modelcolor
Behavior on x { NumberAnimation { duration: 400; easing.type: Easing.OutBack } }
Behavior on y { NumberAnimation { duration: 400; easing.type: Easing.OutBack } }
}
}
}
GridView {
id: grid
anchors.fill: parent
model: appModel
delegate: appDelegate
interactive: false
MouseArea {
anchors.fill: parent
onClicked: {
appModel.insert(1, { "modelcolor": "yellow" })
}
}
}
}
我在幾個地方發現了類似的解決方案,但沒有解釋。你爲什麼需要這個虛擬代表? – meolic 2012-09-04 08:52:44
它可以工作,但是然後GridView不再可以滑動。 – 2015-08-15 10:18:21