Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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.



  • 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.


Log in to reply