이 페이지에서

Qt Quick 예제 - 드래그 앤 드롭

이곳은 QML 드래그 앤 드롭 예제 모음입니다.

드롭 존 사이로 끌려가는 번호가 매겨진 빨간색과 파란색 타일들

'드래그 앤 드롭'은 드래그 앤 드롭 기능과 관련된 간단한 QML 예제 모음입니다. 자세한 내용은 드래그 앤 드롭 페이지를 참조하십시오.

예제 실행

다음에서 예제를 실행할 수 있습니다:

타일

Tiles는 단순한 사각형에 드래그 앤 드롭 기능을 추가하여, 이를 특정 그리드 안으로 드래그할 수 있게 해줍니다.

이 컴포넌트에는 드래그 시 MouseArea 을 사용하여 항목을 이동하는 DragTile 컴포넌트가 포함되어 있습니다:

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
                    }
                }
            }
        }
    }
}

또한, 드래그된 타일을 놓을 수 있는 DropTile 컴포넌트가 있습니다:

DropArea {
    id: dragTarget

    property string colorKey

    width: 64
    height: 64
    keys: [ colorKey ]

    Rectangle {
        id: dropRectangle

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

DropArea 의 keys 속성은 Drag.keys 속성에 일치하는 키가 있는 항목만 해당 위에 놓을 수 있도록 허용합니다.

GridView 예제

GridView 예제는 GridView 에 드래그 앤 드롭 기능을 추가하여, 기본 ListModel 를 변경하지 않고도 델리게이트의 순서를 시각적으로 재정렬할 수 있게 해줍니다. 이 예제는 DelegateModel 를 사용하여 델리게이트 항목을 드래그하여 지나가는 다른 항목의 위치로 이동시킵니다.

    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
            }
        }

예제 프로젝트 @ 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.