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

Reject a Drag in a DragArea



  • I have been trying to learn about Drag and Drop facilities.
    I started with the given example with the red and green Rectangle
    in which the visible property of the green rectangle is made visible
    when the red box enters it drop area.

    I noticed that I could drop the red box in any area of the window, not just
    in the dropArea! Now to learn more I would like to know how to reject
    a "drop". And to have a drop rejected for any area other than the dropArea.
    What I would like to see is that the dropped red box return to its original
    position if the drop is reject. Is this possible.

    I found some code on StackOverflow but it must be too old and causes
    an error when I try to use it.





  • This post is deleted!


  • @lawrence-emke
    i did simplified version of that example,

    ApplicationWindow{
    
        width: 500
        height: 500
        visible: true
    
        Item {
            id: root
            width: 64; height: 64
            MouseArea {
                id: mouseArea
                anchors.fill: parent
                drag.target: tile
                onReleased: parent = tile.Drag.target !== null ? tile.Drag.target : root
                Rectangle {
                    id: tile
                    width: 64; height: 64
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.horizontalCenter: parent.horizontalCenter
                    color: "red"
                    Drag.active: mouseArea.drag.active
                    states: State {
                        when: mouseArea.drag.active
                        ParentChange { target: tile; parent: root }
                        AnchorChanges { target: tile; anchors.verticalCenter: undefined; anchors.horizontalCenter: undefined }
                    }
                }
            }
        }
    
    
        DropArea {
            id: dragTarget
            width: 64; height: 64
            anchors.centerIn: parent
            Rectangle {
                id: dropRectangle
                color: "transparent"
                anchors.fill: parent
                border.width: 2
                border.color: dragTarget.containsDrag ? "grey" : "black"
            }
        }
    
    }
    

Log in to reply