Drag Drop in ListView - DropArea Not Working



  • #####################
    main_DragDrop.qml
    ############################

    import QtQuick 2.0
    import QtQuick.Window 2.0
    import QtQuick.Controls 1.3 as Controls
    import QtQuick.Controls.Styles 1.3
    import QtQml.Models 2.1

    Rectangle {
    id: root
    width: 360
    height: 400
    // anchors.top: parent.top

    DelegateModel {
        id: visualModel
    
        model: PetsModel {}
    	delegate: dragDelegate
      
    }
    
    
    Component {
        id: dragDelegate
    
    	MouseArea {
            id: dragArea
    				 
    		property bool held: false
    
    
    		anchors { left: parent.left; right: parent.right }
    		height: content.height
    
    		drag.target: held ? content : undefined
    		drag.axis: Drag.YAxis
    
    		onPressAndHold: { 
    			console.debug("onPressAndHold")
    			held = true }
    		onReleased: held = false
    
    		Rectangle {
    			id: content
    		
    
    		     anchors {
                    horizontalCenter: parent.horizontalCenter
                    verticalCenter: parent.verticalCenter
                }
                width: dragArea.width; height: column.implicitHeight + 4
    
                border.width: 1
                border.color: "lightsteelblue"
    
    			color: dragArea.held ? "lightsteelblue" : "white"
                Behavior on color { ColorAnimation { duration: 100 } }
    
    			radius: 2
                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: root }
                    AnchorChanges {
                        target: content
                        anchors { horizontalCenter: undefined; verticalCenter: undefined }
                    }
                }
    
    
    			Column {
    					id: column
    					anchors { fill: parent; margins: 2 }
    					Text {	text: name + ' '}
    
    				}
    		DropArea {
    			//anchors { fill: parent; margins: 10 }
    			onDropped: {
    				console.debug("onDropped calling move")
    			
    			}
    
    			onPositionChanged: {
    				console.debug("onPositionChanged calling move")
    			}
    
    
    			onExited : {
    				console.debug("onExited calling move")
    			}
    
    
    			onEntered: {
    				console.debug("onEntered calling move")
    				visualModel.items.move(
    					drag.source.DelegateModel.itemsIndex,
    					dragArea.DelegateModel.itemsIndex)
    				}
    		} // DropArea
    
    		} // content
    
    
    	} // MouseArea dragArea
    } // Component dragDelegate
    

    ListView {
    id: view
    anchors { fill: parent; margins: 2 }
    model: visualModel
    spacing: 15
    cacheBuffer: 50
    }

    } // root
    #################################
    PetsModel.qml
    ###################################

    import QtQuick 2.0

    ListModel {
    ListElement {
    name: "Polly"
    type: "Parrot"
    age: 12
    size: "Small"
    }
    ListElement {
    name: "Penny"
    type: "Turtle"
    age: 4
    size: "Small"
    }
    ListElement {
    name: "Warren"
    type: "Rabbit"
    age: 2
    size: "Small"
    }
    ListElement {
    name: "Spot"
    type: "Dog"
    age: 9
    size: "Medium"
    }
    ListElement {
    name: "Schrödinger"
    type: "Cat"
    age: 2
    size: "Medium"
    }
    ListElement {
    name: "Joey"
    type: "Kangaroo"
    age: 1
    size: "Medium"
    }
    ListElement {
    name: "Kimba"
    type: "Bunny"
    age: 65
    size: "Large"
    }
    ListElement {
    name: "Rover"
    type: "Dog"
    age: 5
    size: "Large"
    }
    ListElement {
    name: "Tiny"
    type: "Elephant"
    age: 15
    size: "Large"
    }
    }

    ########
    main.cpp
    ##########

    #include "qtquick2applicationviewer.h"
    #include <QtGui/QGuiApplication>
    #include <QQmlContext>

    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);

    QQuickView view;
    view.setResizeMode(QQuickView::SizeRootObjectToView);
    
    view.setSource(QUrl("main_DragDrop.qml"));
    view.show();
    return app.exec();
    

    }


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.