Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to disable drag&drop for first Item of a ListView
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fheanor
    wrote on last edited by
    #1

    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 !

    1 Reply Last reply
    0
    • Pradeep P NP Offline
      Pradeep P NP Offline
      Pradeep P N
      wrote on last edited by
      #2

      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.

      Pradeep Nimbalkar.
      Upvote the answer(s) that helped you to solve the issue...
      Keep code clean.

      1 Reply Last reply
      3
      • dheerendraD Online
        dheerendraD Online
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

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

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        4
        • F Offline
          F Offline
          Fheanor
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved