How to drag an item outside a ListView in QML

Henry Fané

I am developing a QML application which basically contains two ListView. I would like to copy a QML item from one ListView to another. I tried to handle this by setting Drag property in the delegate but the item cannot go outside the view when I drag the item, I think the Flickable container handles mouse events.
So, I want to try the following:

  • create a mousearea which overlaps the to ListView
  • create a new object by calling **createComponent() / createObject()**
  • reparent this object to the mousearea
  • handle mouse events in the mousearea till drop

This solution seems to me a little complicated, so do you have a better way to achieve this ?


This was a bad idea and too much complicated. I think I got a way to achieve this:

  • each delegate of the ListView has a hidden Item which can be dragged,
  • as my ListView are in a reusable component, I use a property to pass a higher item (a Rectangle here and NOT a **MouseArea**) which can be used as parent for dragged items,
  • the higher item contains the two ListView (and maybe more in the future),
  • when the drag begins, the item is set to visible and reparented using a **State**

So, I missed the point that set the parent should solve my problem.

Tarod

Next code is just an idea, but the key is to have a MouseArea inside a delegate for the first ListView so the user can drag the items and drop them into a DropArea which belongs to the second ListView.

In this example, model is very simple, just a number. And when the item is dropped, it is removed from the first ListView:

listView.model.remove(listView.dragItemIndex)

Just remove that line of code to copy the item instead of removing.

main.qml

import QtQuick 2.5
import QtQuick.Window 2.2

Window {
    visible: true
    width: 600
    height: 600

    Rectangle {
        id: root
        width: 400
        height: 400

        ListView {
            id: listView
            width: parent.width / 2
            height: parent.height

            property int dragItemIndex: -1

            model: ListModel {
                Component.onCompleted: {
                    for (var i = 0; i < 10; ++i) {
                        append({value: i});
                    }
                }
            }

            delegate: Item {
                id: delegateItem
                width: listView.width
                height: 50

                Rectangle {
                    id: dragRect
                    width: listView.width
                    height: 50
                    anchors.horizontalCenter: parent.horizontalCenter
                    anchors.verticalCenter: parent.verticalCenter
                    color: "salmon"
                    border.color: Qt.darker(color)

                    Text {
                        anchors.centerIn: parent
                        text: modelData
                    }

                    MouseArea {
                        id: mouseArea
                        anchors.fill: parent
                        drag.target: dragRect

                        drag.onActiveChanged: {
                            if (mouseArea.drag.active) {
                                listView.dragItemIndex = index;
                            }
                            dragRect.Drag.drop();
                        }
                    }

                    states: [
                        State {
                            when: dragRect.Drag.active
                            ParentChange {
                                target: dragRect
                                parent: root
                            }

                            AnchorChanges {
                                target: dragRect
                                anchors.horizontalCenter: undefined
                                anchors.verticalCenter: undefined
                            }
                        }
                    ]

                    Drag.active: mouseArea.drag.active
                    Drag.hotSpot.x: dragRect.width / 2
                    Drag.hotSpot.y: dragRect.height / 2
                }
            }
        }

        ListView {
            id: listView2
            width: parent.width / 2
            height: parent.height
            anchors.right: parent.right

            property int dragItemIndex: -1

            DropArea {
                id: dropArea
                anchors.fill: parent
                onDropped: {
                    listView2.model.append(listView.model.get(listView.dragItemIndex))
                    listView.model.remove(listView.dragItemIndex)
                    listView.dragItemIndex = -1;
                }
            }

            model: ListModel {
                Component.onCompleted: {
                    for (var i = 0; i < 1; ++i) {
                        append({value: i});
                    }
                }
            }

            delegate: Item {
                id: delegateItem2
                width: listView2.width
                height: 50

                Rectangle {
                    id: dragRect2
                    width: listView2.width
                    height: 50
                    anchors.horizontalCenter: parent.horizontalCenter
                    anchors.verticalCenter: parent.verticalCenter
                    color: "salmon"
                    border.color: Qt.darker(color)

                    Text {
                        anchors.centerIn: parent
                        text: modelData
                    }
                }
            }
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to make an item drag inside a circle in QML?

From Dev

Item is shown outside of the top and bottom when scorlling the QML ListView

From Dev

Drag and drop item in listview

From Dev

How to drag and drop item into ListView and detect item it was dropped onto

From Dev

How can I access QSortFilterProxyModel elements in QML outside of a ListView

From Dev

Drag and drop item in same ListView

From Dev

Center QML ListView highlighted item

From Dev

Drag drop Listview item into another item

From Dev

QML ListView draws ObjectModel outside of its bounds

From Java

Can QML StackLayout detect drag from a component outside itself

From Dev

QML ListView current item not changing with keystrokes or mouse

From Dev

QML ListView - change all but current item

From Dev

QML ListView current item not changing with keystrokes or mouse

From Dev

QML - ListView item which displays a menu on tap

From Dev

Drag and drop an item from one recyclerview (listview) into another recyclerview (listview)

From Dev

Drag drop ListView Item from one listView to another

From Dev

Drag and drop an item from one recyclerview (listview) into another recyclerview (listview)

From Dev

Drag drop ListView Item from one listView to another

From Dev

Blackberry 10 sdk QML - Changing value of button outside listview

From Dev

How to add a ListView in a ListView item

From Dev

C# delete item from listview from control outside listview

From Dev

setText to a TextView(which is outside a ListView) from a ListView item

From Dev

How to set a button outside listview

From Dev

How to setVisibility in a listview by a button outside it?

From Dev

How to delete the item in listview

From Dev

How to implement Drag&Drop in listview?

From Dev

How to change listview DataSet on Gallery drag?

From Dev

Listview Right-Click Drag After Left-Click Drag Removes Item

From Dev

QtQuick2 - QML reusable item focus changes when selecting/clicking outside/inside of root item

Related Related

  1. 1

    How to make an item drag inside a circle in QML?

  2. 2

    Item is shown outside of the top and bottom when scorlling the QML ListView

  3. 3

    Drag and drop item in listview

  4. 4

    How to drag and drop item into ListView and detect item it was dropped onto

  5. 5

    How can I access QSortFilterProxyModel elements in QML outside of a ListView

  6. 6

    Drag and drop item in same ListView

  7. 7

    Center QML ListView highlighted item

  8. 8

    Drag drop Listview item into another item

  9. 9

    QML ListView draws ObjectModel outside of its bounds

  10. 10

    Can QML StackLayout detect drag from a component outside itself

  11. 11

    QML ListView current item not changing with keystrokes or mouse

  12. 12

    QML ListView - change all but current item

  13. 13

    QML ListView current item not changing with keystrokes or mouse

  14. 14

    QML - ListView item which displays a menu on tap

  15. 15

    Drag and drop an item from one recyclerview (listview) into another recyclerview (listview)

  16. 16

    Drag drop ListView Item from one listView to another

  17. 17

    Drag and drop an item from one recyclerview (listview) into another recyclerview (listview)

  18. 18

    Drag drop ListView Item from one listView to another

  19. 19

    Blackberry 10 sdk QML - Changing value of button outside listview

  20. 20

    How to add a ListView in a ListView item

  21. 21

    C# delete item from listview from control outside listview

  22. 22

    setText to a TextView(which is outside a ListView) from a ListView item

  23. 23

    How to set a button outside listview

  24. 24

    How to setVisibility in a listview by a button outside it?

  25. 25

    How to delete the item in listview

  26. 26

    How to implement Drag&Drop in listview?

  27. 27

    How to change listview DataSet on Gallery drag?

  28. 28

    Listview Right-Click Drag After Left-Click Drag Removes Item

  29. 29

    QtQuick2 - QML reusable item focus changes when selecting/clicking outside/inside of root item

HotTag

Archive