QML Dynamic View Ordering Tutorial 2 - Dragging View Items | Qt Quick (original) (raw)

Now that we have a visible list of items we want to be able to interact with them. We'll start by extending the delegate so the visible content can be dragged up and down the screen. The updated delegate looks like this:

Component {
    id: dragDelegate

    [MouseArea](qml-qtquick-mousearea.html) {
        id: dragArea

        property [bool](qml-bool.html) held: false
        required property [string](qml-string.html) name
        required property [string](qml-string.html) type
        required property [string](qml-string.html) size
        required property [int](qml-int.html) age

        anchors {
            left: parent?.left
            right: parent?.right
        }
        height: content.height

        drag.target: held ? content : undefined
        drag.axis: Drag.YAxis

        onPressAndHold: held = true
        onReleased: held = false

        [Rectangle](qml-qtquick-rectangle.html) {
            id: content
            anchors {
                horizontalCenter: parent.horizontalCenter
                verticalCenter: parent.verticalCenter
            }
            width: dragArea.width
            height: column.implicitHeight + 4

            border.width: 1
            border.color: "lightsteelblue"
            color: dragArea.held ? "lightsteelblue" : "white"
            Behavior on color { [ColorAnimation](qml-qtquick-coloranimation.html) { duration: 100 } }
            radius: 2
            states: State {
                when: dragArea.held

                [ParentChange](qml-qtquick-parentchange.html) {
                    target: content
                    parent: root
                }
                [AnchorChanges](qml-qtquick-anchorchanges.html) {
                    target: content
                    anchors {
                        horizontalCenter: undefined
                        verticalCenter: undefined
                    }
                }
            }
            [Column](qml-qtquick-column.html) {
                id: column
                anchors {
                    fill: parent
                    margins: 2
                }

                [Text](qml-qtquick-text.html) { text: qsTr('Name: ') + dragArea.name }
                [Text](qml-qtquick-text.html) { text: qsTr('Type: ') + dragArea.type }
                [Text](qml-qtquick-text.html) { text: qsTr('Age: ') + dragArea.age }
                [Text](qml-qtquick-text.html) { text: qsTr('Size: ') + dragArea.size }
            }
        }
    }
}
Walkthrough

The major change here is the root item of the delegate is now a MouseArea which provides handlers for mouse events and will allow us to drag the delegate's content item. It also acts as a container for the content item which is important as a delegate's root item is positioned by the view and cannot be moved by other means.

    [MouseArea](qml-qtquick-mousearea.html) {
        id: dragArea

        property [bool](qml-bool.html) held: false
        required property [string](qml-string.html) name
        required property [string](qml-string.html) type
        required property [string](qml-string.html) size
        required property [int](qml-int.html) age

        anchors {
            left: parent?.left
            right: parent?.right
        }
        height: content.height

        drag.target: held ? content : undefined
        drag.axis: Drag.YAxis

        onPressAndHold: held = true
        onReleased: held = false

        [Rectangle](qml-qtquick-rectangle.html) {
            id: content
        }
    }

Dragging the content item is enabled by binding it to the MouseArea's drag.target property. Because we still want the view to be flickable we wait until the MouseArea's pressAndHold signal is emitted before binding the drag target. This way when mouse moves before the hold timeout has expired it is interpreted as moving the list and if it moves after it is interpreted as dragging an item. To make it more obvious to the user when an item can be dragged we'll change the background color of the content item when the timeout has expired.

            color: dragArea.held ? "lightsteelblue" : "white"
            Behavior on color { [ColorAnimation](qml-qtquick-coloranimation.html) { duration: 100 } }

The other thing we'll need to do before an item can be dragged is to unset any anchors on the content item so it can be freely moved around. We do this in a state change that is triggered when the delegate item is held, at the same time we can reparent the content item to the root item so that is above other items in the stacking order and isn't obscured as it is dragged around.

            states: State {
                when: dragArea.held

                [ParentChange](qml-qtquick-parentchange.html) {
                    target: content
                    parent: root
                }
                [AnchorChanges](qml-qtquick-anchorchanges.html) {
                    target: content
                    anchors {
                        horizontalCenter: undefined
                        verticalCenter: undefined
                    }
                }
            }

Example project @ code.qt.io

A Simple ListView and Delegate Moving Dragged Items

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.