如何交换 qml 网格中的元素?

发布于 2024-12-04 13:55:01 字数 257 浏览 1 评论 0原文

我尝试过:

var child = grid.children[slot1];
grid.children[slot1] = grid.children[slot2];
grid.children[slot2] = child;

但没有成功。

当将元素重新设置为网格的父级(element.parent = grid)时,它会被有效地添加,但无法对它们进行排序。

我应该使用自己的 QML 小部件吗?

I tried :

var child = grid.children[slot1];
grid.children[slot1] = grid.children[slot2];
grid.children[slot2] = child;

But it didn't work.

When reparenting an element to the grid (element.parent = grid), it's effectively added, but there's no way to order them.

Should i use my own QML widget?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

季末如歌 2024-12-11 13:55:01

ListModel 提供移动元素功能,您可以将其与 GridView(不仅仅是网格)结合使用。

GridView {
    id: grid
    anchors.fill: parent
    cellWidth: 32
    cellHeight: 32
    property bool loaded: false;

    model : ModelInheritingListModel {
    }

    delegate: MyDelegate {

    }

    onSwap: {
        var min = Math.min(slot1, slot2);
        var max = Math.max(slot1, slot2);
        grid.model.move(min, max, 1);
        grid.model.move(max-1, min, 1);
    }
}

如果您想在委托中添加动画,则在交换时:

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!

ListModel provides moving elements functionality, and you can use it in conjunction with a GridView (not just a grid).

GridView {
    id: grid
    anchors.fill: parent
    cellWidth: 32
    cellHeight: 32
    property bool loaded: false;

    model : ModelInheritingListModel {
    }

    delegate: MyDelegate {

    }

    onSwap: {
        var min = Math.min(slot1, slot2);
        var max = Math.max(slot1, slot2);
        grid.model.move(min, max, 1);
        grid.model.move(max-1, min, 1);
    }
}

And if you want to add animations in the delegate, when swapping:

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
    }
}

Notice the trick you have to use by reparenting...

Damn QML!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文