当前位置 : 主页 > 网络推广 > seo >

qt – 如何在拖放事件后检索ListView中ListModel的新有序列表

来源:互联网 收集:自由互联 发布时间:2021-06-16
我有这个简单的qml代码 import QtQuick 2.7import QtQuick.Controls 2.0import QtQuick.Layouts 1.0import QtQml.Models 2.2ApplicationWindow { visible: true width: 300 height: 120 title: qsTr("Hello World") Rectangle { anchors.fill: paren
我有这个简单的qml代码

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
import QtQml.Models 2.2

ApplicationWindow {
    visible: true
    width: 300
    height: 120
    title: qsTr("Hello World")
    Rectangle {
        anchors.fill: parent;

        ListView{
            id: timeline
            anchors.fill: parent
            orientation: ListView.Horizontal
            model: visualModel
            delegate: timelineDelegate

            moveDisplaced: Transition {
                NumberAnimation{
                    properties: "x,y"
                    duration: 200
                }
            }

            DelegateModel {
                id: visualModel
                model: timelineModel
                delegate: timelineDelegate
            }

            Component {
                id: timelineDelegate


                MouseArea {
                    id: dragArea

                    width: 100; height: 100

                    property bool held: false

                    drag.target: held ? content : undefined
                    drag.axis: Drag.XAxis

                    onPressAndHold: held = true
                    onReleased: {
                        held = false
                        var listOnModel = "{";
                        for(var i = 0; i < timelineModel.count; i++){
                            listOnModel += timelineModel.get(i).colore + ", "
                        }
                        console.log("List: " + listOnModel + "}");
                    }

                    Rectangle {
                        id: content

                        anchors { horizontalCenter: parent.horizontalCenter; verticalCenter: parent.verticalCenter }
                        width: 100
                        height: 100

                        color: colore
                        opacity: dragArea.held ? 0.8 : 1.0

                        Text{
                            anchors.verticalCenter: parent.verticalCenter
                            anchors.horizontalCenter: parent.horizontalCenter
                            text: index
                            font.pixelSize: 20
                        }

                        Drag.active: dragArea.held
                        Drag.source: dragArea
                        Drag.hotSpot.x: width / 2
                        Drag.hotSpot.y: height / 2

                        states: State{
                            when: dragArea.held
                            ParentChange { target: content; parent: timeline }
                            AnchorChanges {
                                target: content
                                anchors { horizontalCenter: undefined; verticalCenter: undefined }
                            }
                        }
                    }

                    DropArea {
                        anchors.fill: parent
                        onEntered: {
                            visualModel.items.move( drag.source.DelegateModel.itemsIndex, dragArea.DelegateModel.itemsIndex);
                        }

                    }
                }
            }

            ListModel {
                id: timelineModel
                // @disable-check M16
                ListElement { colore: "blue" }
                // @disable-check M16
                ListElement { colore: "orange" }
                // @disable-check M16
                ListElement { colore: "green" }
            }
        }
    }
}

这里我们有一个简单的彩色可拖动矩形列表.在每个矩形的中心显示该组件在模型内部具有的实际索引.

如您所见,在drop事件之后,每个项目的索引都不会更改,并且模型中项目的顺序仍然相同.有没有办法在拖放事件发生后检索列表的新顺序?

您不重新排序ListModel,而是重新排序DelegateModel的项目.
所以你需要使用这个代码:

onReleased: {
    held = false
    var listOnModel = "{";
    for(var i = 0; i < visualModel.items.count; i++){
        listOnModel += visualModel.items.get(i).model.colore + ", "
    }
    console.log("List: " + listOnModel + "}");
}
网友评论