如何在qml网格中交换元素?
我试过:如何在qml网格中交换元素?,qml,Qml,我试过: var child = grid.children[slot1]; grid.children[slot1] = grid.children[slot2]; grid.children[slot2] = child; 但它不起作用 将元素重新出租到网格(element.parent=grid)时,会有效地添加元素,但无法对其排序 我应该使用自己的QML小部件吗?ListModel提供了移动元素功能,您可以将其与GridView(而不仅仅是网格)结合使用 如果要在代理中添加动画,则在交
var child = grid.children[slot1];
grid.children[slot1] = grid.children[slot2];
grid.children[slot2] = child;
但它不起作用
将元素重新出租到网格(element.parent=grid)时,会有效地添加元素,但无法对其排序
我应该使用自己的QML小部件吗?ListModel提供了移动元素功能,您可以将其与GridView(而不仅仅是网格)结合使用 如果要在代理中添加动画,则在交换时:
Item {
id: main
width: grid.cellWidth
height: grid.cellHeight
Image {
id: img
x: main.x
y: main.y
/* In order to use animations, we can't use the main level component as
it technically is the same item, so we need to animate the image.
So we set the image's position relative to the parent instead, so
we can animate its coordinates properly */
parent: grid
Behavior on x { NumberAnimation { duration: 400; easing.type: Easing.InOutCubic}}
Behavior on y { NumberAnimation { duration: 400; easing.type: Easing.InOutCubic}}
source: imagesource
width: 32
height: 32
}
}
注意你必须使用的技巧,重新租用
该死的QML
Item {
id: main
width: grid.cellWidth
height: grid.cellHeight
Image {
id: img
x: main.x
y: main.y
/* In order to use animations, we can't use the main level component as
it technically is the same item, so we need to animate the image.
So we set the image's position relative to the parent instead, so
we can animate its coordinates properly */
parent: grid
Behavior on x { NumberAnimation { duration: 400; easing.type: Easing.InOutCubic}}
Behavior on y { NumberAnimation { duration: 400; easing.type: Easing.InOutCubic}}
source: imagesource
width: 32
height: 32
}
}