[Interest] Cannot drag a Pane in Qml even on Qt 6.X

EXT Mitch Curtis mitch.curtis at qt.io
Tue Mar 7 01:28:35 CET 2023


> -----Original Message-----
> From: Interest <interest-bounces at qt-project.org> On Behalf Of Nuno
> Santos
> Sent: Monday, 6 March 2023 4:55 PM
> To: Qt Interest <interest at qt-project.org>
> Subject: [Interest] Cannot drag a Pane in Qml even on Qt 6.X
> 
> Hi,
> 
> It was not possible to drag a Pane on Qt 5.X
> 
> But I was almost convinced it was possible to do it on Qt 6.X
> 
> But now I have tried again and it seems it is not possible. Or maybe I’m doing
> something wrong.
> 
> Is there any strong reason a Pane cannot be draggable?

The parent of the MouseArea is the contentItem, thanks to contentData being the default property. That's explained here:

https://doc.qt.io/qt-6/qml-qtquick-controls2-pane.html#details

You can see what really happens by modifying the code below to set the drag target to parent again and uncomment the title binding - the contentItem (and black-bordered rectangle) will move but the Pane itself won't.

Set the drag target to the pane and it will work:

    import QtQuick
    import QtQuick.Controls

    Window {
        width: 640
        height: 480
        visible: true
        title: "pane.x: " + pane.x + " pane.y: " + pane.y
    //    title: "pane.contentItem.x: " + pane.contentItem.x + "pane.contentItem.y: " + pane.contentItem.y

        Item {
            width: 200
            height: 200

            DropArea {
                x: 75
                y: 75
                width: 50
                height: 50

                Rectangle {
                    anchors.fill: parent
                    color: parent.containsDrag ? "green" : "gray"
                }
            }

            Pane {
                id: pane
                x: 10
                y: 10
                width: 20
                height: 20
                leftPadding: 0
                rightPadding: 0
                topPadding: 0
                bottomPadding: 0
                background: Rectangle {
                    color: "tomato"
                }

                Rectangle {
                    anchors.fill: parent
                    color: "transparent"
                    border.color: "black"
                }

                Drag.active: dragArea.drag.active
                Drag.hotSpot.x: 10
                Drag.hotSpot.y: 10

                MouseArea {
                    id: dragArea
                    anchors.fill: parent

                    drag.target: pane
                }
            }
        }
    }

> Thanks!
> 
> Nuno
> 
> 
> import QtQuick
> import QtQuick.Layouts
> import QtQuick.Controls
> 
> Window {
>     width: 640
>     height: 480
>     visible: true
>     title: qsTr("Hello World")
> 
>     Item {
>         width: 200; height: 200
> 
>         DropArea {
>             x: 75; y: 75
>             width: 50; height: 50
> 
>             Rectangle {
>                 anchors.fill: parent
>                 color: parent.containsDrag ? "green" : "gray"
>             }
>         }
> 
>         //Rectangle {
>         Pane {
>             x: 10; y: 10
>             width: 20; height: 20
>             //color: "red"
> 
>             Drag.active: dragArea.drag.active
>             Drag.hotSpot.x: 10
>             Drag.hotSpot.y: 10
> 
>             MouseArea {
>                 id: dragArea
>                 anchors.fill: parent
> 
>                 drag.target: parent
>             }
>         }
>     }
> }
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest


More information about the Interest mailing list