[Qt-qml] Positioner reordering children

Colin Kern colin.kern at gmail.com
Tue Oct 19 05:56:01 CEST 2010


On Mon, Oct 18, 2010 at 10:14 PM, Colin Kern <colin.kern at gmail.com> wrote:
> I see that Positioners have add and move events, but from reading the
> documentation it only mentions the move event being used when an item
> is added or removed. Is there a way to reorder items and have it
> animated, or is this something I'd have to implement myself?

I ended up doing my own implementation, but I'd still like to know if
there's a built-in way.  Here's my implementation of a vertical layout
with reordering:

List.qml:
Item {
    function itemClicked(i) {
        for (var j = 0; j < children.length; ++j) {
            if (children[j].i < i) {
                ++children[j].i;
            } else if (children[j].i == i) {
                children[j].i = 0;
            }
        }
    }
}

ListItem.qml:
Item {
    property int i

    anchors.horizontalCenter: parent.horizontalCenter
    y: i * height

    MouseArea {
        anchors.fill: parent
        onClicked: parent.parent.itemClicked(i)
    }

    Behavior on y {
        NumberAnimation { duration: 500 }
    }
}

It's not a very general solution, for example it assumes that all the
ListItems in a List will have the same height, but the functionality
of reordering items is working. In this case an item is moved to the
top when it's clicked.

Thanks,
Colin



More information about the Qt-qml mailing list