Search code examples
qtqmldrag

QML Drag on Rectangle doesn't capture events


i'm trying to accomplish a simple drag of a Image that fires an event when the Drag is finished or started.

Rectangle {
    id: upperFooter
    color: "transparent"
    Layout.alignment: Qt.AlignLeft | Qt.AlignBottom
    Drag.onDragFinished: onDragFinished()
    Drag.dragType: Drag.Automatic
    width: 800
    height: 40
    Image {
        height: 40
        width: upperFooter.width
        source: "qrc:///images/footer/background_footer.svg"
        MouseArea {
            id: iconMouseArea
            x: 390
            y: 10
            anchors.fill: parent
            onClicked: toggleHiddenBar()
            drag {
                target: upperFooter
                axis: Drag.YAxis

            }
        }
    }

The event on the upperFooter item doesn't fire.

QML: onDragStarted / finished not called even though the drag property is active --> I've tried to put Drag.type on Drag.Automatic to solve this but still doesn't work.

QtQuick v 2.6 Layout 1.3 QML 5.6


Solution

  • I solved this issue by adding explicit dragActive property to the MouseArea. So in Your case code will be:

    MouseArea {
        id: iconMouseArea
        drag {
            target: upperFooter
            axis: Drag.YAxis
        }
    
        property bool dragActive: drag.active
    
        onDragActiveChanged: {
            if(drag.active) { //
                ... // Dragging started
            } else {
                ... // Dragging finished
            }
        }
    }