Nesta página

Qt Quick Exemplos - Arrastar e soltar

Esta é uma coleção de exemplos de arrastar e soltar em QML.

Peças numeradas nas cores vermelha e azul sendo arrastadas entre áreas de destino

“Arrastar e soltar” é uma coleção de pequenos exemplos em QML relacionados à funcionalidade de arrastar e soltar. Para mais informações, acesse a página “Arrastar e soltar ”.

Executando o exemplo

Você pode executar o exemplo a partir de:

Tiles

O Tiles adiciona a funcionalidade de arrastar e soltar a retângulos simples, que você pode arrastar para uma grade específica.

Possui um componente DragTile que utiliza um MouseArea para mover um item quando arrastado:

Item {
    id: root

    required property string colorKey
    required property int modelData

    width: 64
    height: 64

    MouseArea {
        id: mouseArea

        width: 64
        height: 64
        anchors.centerIn: parent

        drag.target: tile

        onReleased: parent = tile.Drag.target !== null ? tile.Drag.target : root

        Rectangle {
            id: tile

            width: 64
            height: 64
            anchors {
                verticalCenter: parent.verticalCenter
                horizontalCenter: parent.horizontalCenter
            }

            color: root.colorKey

            Drag.keys: [ root.colorKey ]
            Drag.active: mouseArea.drag.active
            Drag.hotSpot.x: 32
            Drag.hotSpot.y: 32
            states: State {
                when: mouseArea.drag.active
                AnchorChanges {
                    target: tile
                    anchors {
                        verticalCenter: undefined
                        horizontalCenter: undefined
                    }
                }
            }
        }
    }
}

E um componente `DropTile`, no qual os blocos arrastados podem ser soltos:

DropArea {
    id: dragTarget

    property string colorKey

    width: 64
    height: 64
    keys: [ colorKey ]

    Rectangle {
        id: dropRectangle

        anchors.fill: parent
        color: dragTarget.containsDrag ? "grey" : dragTarget.colorKey
    }
}

A propriedade `keys` do ` DropArea ` só permitirá que um item seja solto nele se ele tiver uma chave correspondente na propriedade `Drag.keys`.

Exemplo de GridView

O Exemplo do `GridView ` adiciona a funcionalidade de arrastar e soltar a um ` GridView`, permitindo que você reordene visualmente os delegados sem alterar a ` ListModel` subjacente. Ele usa um ` DelegateModel ` para mover um item delegado para a posição de outro item sobre o qual ele é arrastado.

    model: DelegateModel {
        delegate: DropArea {
            id: delegateRoot
            required property color color

            width: 80
            height: 80

            onEntered: function(drag) {
                visualModel.items.move((drag.source as Icon).visualIndex, icon.visualIndex)
            }

            property int visualIndex: DelegateModel.itemsIndex

            Icon {
                id: icon
                dragParent: root
                visualIndex: delegateRoot.visualIndex
                color: delegateRoot.color
            }
        }

Projeto de exemplo @ code.qt.io

© 2026 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.