如何重新排序 QML 行的 children(使用拖放)?

How to reorder children of QML Row (using drag and drop)?

比方说,我有一个 QML Row,它有一些 children objects(不一定是同一类型)。我希望能够随意重新排序。好处是,能够为用户实现拖放行为。这是一个代码示例,Row 包含我想重新排序的不同组件:

import QtQuick 2.5
import QtQuick.Controls 1.4


ApplicationWindow {
    visible: true
    width: 300
    height: 300

    Component {
        id: rect
        Rectangle {
            color: "blue"
        }
    }

    Component {
        id: rectWithText

        Rectangle {
            property alias text: txt.text
            color: "red"
            Text {
                id: txt
            }
        }
    }

    Row {
        id: r
        Component.onCompleted: {
            rect.createObject(r,{"width": 60,"height":40})
            rectWithText.createObject(r,{"width": 100,"height":40,text:"foo"})
            rect.createObject(r,{"width": 40,"height":40})
        }
    }
}

QtWidgets中有layout->removeWidget(widget)layout->insertWidget(index,widget)等函数,QML中好像没有。

我找到了一些使用 ListView/GridView 和 model/delegate 方法的示例,但它们通常无法处理不同的组件,因此它们无法替代Row.

有没有办法在 QML 中做到这一点,还是我运气不好?

import QtQuick 2.3
import QtQuick.Window 2.2
import QtQuick.Controls 1.1

Window {
    visible: true

    Component {
        id: buttonComponent
        Button { text: "I'm a button" }
    }

    Component {
        id: labelComponent
        Label { text: "I'm a label" }
    }

    property var buttonModel: [buttonComponent, labelComponent, buttonComponent, buttonComponent,labelComponent]

    function shuffle(array) {
        for (var i = array.length - 1; i > 0; i--) {
            var j = Math.floor(Math.random() * (i + 1));
            var temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }
        return array;
    }


    Column {
        anchors.fill: parent
        anchors.centerIn: parent
        Repeater {
            model: buttonModel
            Loader {
                sourceComponent: modelData
            }
        }
        Button {
            text: "shuffle"
            onClicked: buttonModel = shuffle(buttonModel)
        }
    }
}

你也可以用同样的方式使用ListView。这使您在为 Items.

设置动画时更加灵活

这有点乱七八糟,但无论如何都有效。它不需要使用模型。

import QtQuick 2.3
import QtQuick.Window 2.2
import QtQuick.Controls 1.1

Window {
    visible: true
    function shuffle(array) {
        for (var i = array.length - 1; i > 0; i--) {
            var j = Math.floor(Math.random() * (i + 1));
            var temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }
        return array;
    }
    Column {
        id: column
        anchors.fill: parent
        anchors.centerIn: parent
        Button {
            text: "shuffle"
            onClicked: {
                var array = [button1, button2, button3]
                for (var a in array) { array[a].parent = null }
                array = shuffle(array)
                for (a in array) { array[a].parent = column }
            }
        }
        Button {
            id: button1
            text: "I am button 1"
        }
        Button {
            id: button2
            text: "I am button 2 "
        }
        Button {
            id: button3
            text: "I am button 3"
        }
    }
}