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. How to get new parent, when DragHandler is used ?

How to get new parent, when DragHandler is used ?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 356 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.
  • S Offline
    S Offline
    squareroot13
    wrote on last edited by squareroot13
    #1

    Hello, How to get(detect) new parent for draggable item, when DragHandler is used after dropping ?
    I can get new parent for MouseArea when i use MouseArea as Drag Manager(which contains draggable Item) (accrording to examples) , but i can't use '"draggable item(dragrect) within MouseArea" way because TapHandler within draggable Item (dragrect) blocks events to "Drag Manager" MouseArea

    P.S. I can change special property (parentItem) from droping event handler (DropOut::onEntered) of draggable object, but i want do it int draggable event handler because user may cancel dragging, so i need to detect new parent in draggable object when it enter in potentioal parent area

    
    import QtQuick
    import QtQuick 2.15
    import QtQuick.Controls 2.15
    import QtQuick.Shapes 1.9
    import QtQuick.Layouts 1.0
    
    Window {
        id: window
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
        Rectangle {
            id: mainrect
            anchors.fill: parent
        ColumnLayout {
            anchors.fill: parent
            Rectangle {
                Layout.fillWidth: true
                Layout.minimumHeight: 200
                color: "green"
                DropArea {
                    id: srcrect
                    anchors.fill: parent
                    Item {
                        id: marea
                        anchors.fill: parent
                        Rectangle {
                            id: dragrect
                            anchors.verticalCenter: parent.verticalCenter
                            anchors.horizontalCenter: parent.horizontalCenter
                            color: "red"
                            width: 60
                            height: 80
                            z: 20
                            DragHandler {
                              id: dhandler
                            }
                            property Item parentItem: marea
                            states: State {
                                when: dhandler.active
                                PropertyChanges {
                                    target: dragrect
                                    parent: parentItem
                                }
    
                                AnchorChanges {
                                    target: dragrect
                                    anchors {
                                        verticalCenter: undefined
                                        horizontalCenter: undefined
                                    }
                                }
                            }
    
                            Drag.active: dhandler.active
                            Drag.hotSpot.x: width/2
                            Drag.hotSpot.y: height/2
                            property bool dactive: dhandler.active
                            onDactiveChanged: {
                                if (!dactive) {
                                    console.log("!dactive")
                                    parent = Drag.target !== null ? Drag.target : parentItem
                                    if (Drag.target !== null) {
                                        parentItem = Drag.target
                                        console.log("new parentItem")
                                    }
                                }
                                if (dactive) {
                                    console.log("dactive")
                                    parent = Drag.target !== null ? Drag.target : parentItem
                                    if (Drag.target !== null) {
                                        parentItem = Drag.target
                                        console.log("new parentItem")
                                    }
                                }
                            }
    
                            MouseArea {
                                anchors.fill: parent
                                enabled: false
                                drag.target: dragrect
                                propagateComposedEvents: true
                                onReleased:(mouse)=> {
                                    console.log("test")
                                    mouse.accepted = false
                                }
                                onPressed: (mouse)=> {
                                       console.log("test")
                                       mouse.accepted = false
                                }
                            }
                            TapHandler {
                                enabled: true
    
                                //anchors.fill: parent
                                onTapped: function(mouse) {
                                    console.log("tap")
                                    mouse.accepted = false
                                }
                            }
    
                        }
                    }
                }
            }
            Rectangle {
                Layout.fillWidth: true
                Layout.minimumHeight: 200
                color: gggg.containsDrag ? "grey" : "blue"
                //opacity: 0.7
                DropArea {
                    id: gggg
                    anchors.fill: parent
                    onDropped: {
                        console.log("dropped")
                    }
                }
            }
        }
        }
    }
    
    
    1 Reply Last reply
    0
    • Axel SpoerlA Offline
      Axel SpoerlA Offline
      Axel Spoerl
      Moderators
      wrote on last edited by
      #2

      Since items can only be dropped on a drop area, just connect to all drop areas' dropped signal. The signal is emitted with a drag event argument, from which you can establish the source, e.g. the dragged object.

      Have in mind though, that a drop doesn't cause reparenting automatically. So technically, there is no new parent, unless the app sets it explicitly.

      Software Engineer
      The Qt Company, Oslo

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved