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. Dragging Issue: Draggable keeps shifting position after dragging from one location to another
Forum Updated to NodeBB v4.3 + New Features

Dragging Issue: Draggable keeps shifting position after dragging from one location to another

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 1 Posters 207 Views 1 Watching
  • 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.
  • fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on last edited by
    #1

    This code illustrates the problem:

    import QtQuick 2.12
    import QtQuick.Window 2.12
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Dragon Drop: drag to window")
    
        ColorAnimation on color {
            id: rejectAnimation
    
            running: false
            from: "#FCC"
            to: "#EEE"
            duration: 1000
        }
    
        DropArea {
            id: dropArea
            anchors.fill: parent
            keys: ["amet/pathkey"]
            onEntered: (drag) => {
                if(drag.proposedAction != Qt.CopyAction){
                    drag.accepted = false
                    rejectAnimation.start()
                }
            }
            onDropped: (drop) => {
                var textdata = drop.getDataAsString(keys[0])
                if (textdata.length) {
                    if (drop.proposedAction == Qt.CopyAction) {
                        console.log(textdata)
                        drop.acceptProposedAction()
                    }
                }
            }
        }
    
        Window {
            id: side_window
    
            title: "drag from window"
            visible: true
    
            width: 400
            height: 400
    
            MouseArea {
                id: mouseArea
                anchors.fill: parent
                drag.target: draggable
                onPressed: draggable.grabToImage(function(result) {
                    draggable.Drag.imageSource = result.url
                })
            }
            Rectangle {
                id: item
    
                property string display: "data"
    
                Rectangle {
                    id: draggable
    
                    width: 32
                    height: 32
    
                    Drag.active: mouseArea.drag.active
                    Drag.hotSpot.x: 0
                    Drag.hotSpot.y: 0
                    Drag.mimeData: { "amet/pathkey": "text data" }
                    Drag.dragType: Drag.Automatic
                    Drag.supportedActions: Qt.CopyAction
                    Drag.onDragFinished: (dropAction) => {
                        //if (dropAction == Qt.MoveAction)
                        //    item.display = ""
                        draggable.x = 0
                        draggable.y = 0
                    }
    
                    color: "white"
                    border.width: 1
                }
            }
        }
    }
    

    The draggable (square box in the drag from window) keeps shifting position after a drag. I don't want it to move. I cannot find the magic setting to keep this from happening. If this drag would not shift it would be perfectly fine.

    I previously was using Qt.Internal for dragType, but it has more problems dragging between windows. Dragging within one window it is okay.

    Frustrated as both drag types seem to have strange issues. This shifting of the draggable is strange.

    C++ is a perfectly valid school of magic.

    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #2

      Wow, why do I find it when I send a message? Anyway, I found a somewhat hacky fix:

                   Rectangle {
                      id: draggable
      
                      width: 32
                      height: 32
      
                      Drag.active: mouseArea.drag.active
                      Drag.hotSpot.x: 0
                      Drag.hotSpot.y: 0
                      Drag.mimeData: { "amet/pathkey": "text data" }
                      Drag.dragType: Drag.Automatic
                      Drag.supportedActions: Qt.CopyAction
                      Drag.onDragFinished: (dropAction) => {
                          Qt.callLater(fixpos, x,y)
                      }
      
                      function fixpos(x, y){
                          draggable.x = x
                          draggable.y =y
                      }
      
                      color: "white"
                      border.width: 1
                  }
      

      Use callLater to set the x and y to the original x and y of the predrag operation. This seems to postfix the problem fine.

      C++ is a perfectly valid school of magic.

      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