Qt Quick 예제 - 드래그 앤 드롭

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

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

예제 실행하기

에서 예제를 실행하려면 Qt Creator에서 Welcome 모드를 열고 Examples 에서 예제를 선택합니다. 자세한 내용은 예제 빌드 및 실행하기를 참조하세요.

타일

타일은 단순한 직사각형에 드래그 앤 드롭 기능을 추가하여 특정 그리드에 드래그할 수 있습니다.

여기에는 드래그할 때 항목을 이동하기 위해 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 에 드래그 앤 드롭을 추가하여 기본 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

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