Как поменять местами элементы в сетке qml?

Я старался :

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

Но это не сработало.

При переназначении элемента в сетку (element.parent = grid) он фактически добавляется, но нет возможности упорядочить их.

Должен ли я использовать свой собственный виджет QML?


qml
person coyotte508    schedule 13.09.2011    source источник


Ответы (1)


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!

person coyotte508    schedule 14.09.2011