2016-12-06 45 views
6

Mam ten prosty kod QMLJak pobrać nową uporządkowaną listę ListModel wewnątrz ListView po przeciągnij i upuść zdarzenia

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

Tutaj mamy prostą listę kolorowych Draggable prostokątów. W środku każdego prostokąta jest pokazany rzeczywisty index, który ten komponent ma wewnątrz modelu.

enter image description here

Jak widać, po zdarzeniu upuszczania, indeks dla każdej pozycji nie zmienia, a kolejność elementów wewnątrz modelu jest nadal taka sama. Czy istnieje sposób na odzyskanie nowej kolejności listy po wystąpieniu zdarzenia przeciągania i upuszczania?

Odpowiedz

8

Nie zmieniasz kolejności ListModel, ale items swojej DelegateModel. Musisz więc użyć tego kodu:

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 + "}"); 
}