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. Dragging an dynamically created object out of a ListView

Dragging an dynamically created object out of a ListView

Scheduled Pinned Locked Moved QML and Qt Quick
1 Posts 1 Posters 543 Views
  • 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.
  • K Offline
    K Offline
    Kysymys
    wrote on last edited by
    #1

    Hi there,

    I want to drag an object out of a ListView and then later on use it for something.

    • in the ListView is a set of objects
    • dragging is initiated by press and hold
    • the list item is removed from the list
    • the dragged object can be dragged around (aka used for something)

    The problem is, that I can't get the dragged object to move immediately after the press and hold. I always have to release the mouse and press the generated object again to get it moving.
    @// DragItem.qml
    import QtQuick 2.0

    Rectangle {
    width: 100; height: 100
    border.width: 1
    MouseArea {
    anchors.fill: parent
    drag.target: parent
    onPressed: parent.color = "purple"
    onReleased: parent.color = "orange"
    }
    }
    @
    @// Main.qml
    import QtQuick 2.0

    Item {
    id: root
    width: 400; height: 400

    ListView {
        width: 100; height: 400
        spacing: 10
    
        model: ListModel {
            id: listModel
            ListElement { color: "blue" }
            ListElement { color: "yellow" }
            ListElement { color: "red" }
        }
    
        delegate: Rectangle {
            width: 100; height: 100
            color: model.color
            MouseArea {
                anchors.fill: parent
                onPressAndHold: {
                    var position = mapToItem(root, x, y)
                    var component = Qt.createComponent("DragItem.qml")
                    var properties = {
                        "x": position.x, "y": position.y,
                        "color": model.color
                    }
                    component.createObject(root, properties)
                    listModel.remove(index)
                }
            }
        }
    }
    

    }
    @

    \o/ \o/ Kudos ...

    Paholaisen Kysymys

    1 Reply Last reply
    0

    • Login

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