Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在qml网格中交换元素?_Qml - Fatal编程技术网

如何在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
    }
}