How to disable drag&drop for first Item of a ListView



  • I have a QML project where I am able to drag & drop rectangles that are in a ListView.
    I want to disable the drag&drop feature for the first Item (rectangle) of the ListView.

    Here is an example:

    Rectangle {
        visible: true
        width: 1000; height: 1000
        ListView {
            id: root
            width: parent.width; height: parent.height
    
            model: DelegateModel {
    
                id: visualModel
                model: myModel
                model: ListModel {
                    id: colorModel
                    ListElement { someData }
                    ...
    
                }
    
                delegate: MouseArea {
    
                    property int visualIndex: DelegateModel.itemsIndex
    
                    id: delegateRoot
                    cursorShape: Qt.PointingHandCursor
                    width: root.width; height: 100
                    drag.target: icon
                    drag.axis: Drag.YAxis
                    drag.minimumY: 0
    
                    Rectangle {
                      blablaData
    
                     //Something like : if firstItem, disable drag&drop
                    }
    
    
                    DropArea {
                        anchors { fill: parent; margins: 15 }
    
                        onEntered: {                   
                              visualModel.items.move(drag.source.visualIndex, delegateRoot.visualIndex)               
                        }
                     }
                 }
              }
        }
    }
    

    Do you have any idea of how to do it ?
    Thanks a lot !



  • Hi Fheanor,

    You can try this.

    DelegateModel {
            id: del_Model
            model: MyModel {}
            delegate: dragDelegate
        }
    
        Component {
            id: dragDelegate
            MouseArea {
                id: dragArea
    
                property bool held: false
                property bool dragging: drag.active
                anchors { left: parent.left; right: parent.right }
                height: content.height
    
                drag.target: held == true ? content : undefined
                drag.axis: held == true  ? Drag.YAxis : Drag.None
                
                onPressAndHold: {
                    if( index > 0){
                        held = true
                    }
                }
    
                Rectangle {
                    id: content         
                  // Some Data
                 }
    
                DropArea {
                    id : _dropArea
                    anchors { fill: dragArea;  margins: 50 }
    
                    onEntered: {
                        var a = dragArea.DelegateModel.itemsIndex
                        var b = drag.source.DelegateModel.itemsIndex
    
                        if(a > 0 && b > 0){
                            del_Model.items.move(a, b, 1)
                        }
                    }
                }
            }
        }
    

    The element of list is not enabled for drag&drop.



  • In addition to Pradeep said, there is index parameter in each delegate object. You can use that value to enable and disable.



  • Thank you very much, this was exactly what I needed !
    Have a good day


Log in to reply
 

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