【问题标题】:View Item gets dropped outside drop area in qml查看项目在 qml 中被丢弃在放置区域之外
【发布时间】:2016-10-17 11:43:03
【问题描述】:

我实现了一个 Gridview,其中包含不同的图像,我需要将项目拖出网格视图并将其放置在 DropArea 中。我关注了How to drag an item outside a ListView in QML 但现在我的网格项目可以在页面的任何地方拖动。我需要将其限制为 DropArea。 它还存在网格项目的分层问题。我正在尝试将拖动的图块带到最顶层的视图 我对 Qt 完全陌生

这是我的代码

import QtQuick 2.0

Item {

    id: media_item
    anchors.fill: parent



    Rectangle
    {
        y: 100
        id: holder_box
        width: 450
        height: 150
        color: "#99000000"
        radius: 5

        GridView {
            id: grid
            anchors.fill: parent
            model: mediaModel
            delegate:  Item {
                id: main_item
                width: grid.cellWidth;
                height: grid.cellHeight


                Rectangle {
                    id: item; parent: grid
                    color: mediagraphic
                    x: main_item.x; y: main_item.y
                    width: 100; height: 100;

                    Behavior on x { NumberAnimation { duration: 400; easing.type: Easing.OutBack } }
                    Behavior on y { NumberAnimation { duration: 400; easing.type: Easing.OutBack } }
                }
                Drag.active: mouseArea.drag.active
                Drag.hotSpot.x: main_item.width / 2
                Drag.hotSpot.y: main_item.height / 2


                MouseArea {
                    id: mouseArea
                    anchors.fill: main_item
                    drag.target: main_item

                    drag.onActiveChanged: {
                        if (mouseArea.drag.active) {
                            grid.dragItemIndex = index;
                        }
                        main_item.Drag.drop();
                    }
                }

                states: [

                    State {
                        name: 'dragged'
                        when: main_item.Drag.active
                        PropertyChanges {
                            target: main_item
                            x: x
                            y: y
                        }
                        ParentChange
                        {
                            target: main_item
                            parent: topDrag
                        }
                    }
                ]

                onStateChanged: console.log('State', state)
            }
            interactive: false
            anchors.topMargin: -30

            property int dragItemIndex: -1

        }




        ListModel {
            id: mediaModel
            ListElement { mediagraphic: "orchid"
                songname: "Song 1"}
            ListElement {mediagraphic: "pink"
                songname: "Song 2"}
            ListElement {mediagraphic: "purple"
                songname: "Song 3"}
            ListElement {mediagraphic: "lighpink"
                songname: "Song 4"}
            ListElement {mediagraphic: "blue"
                songname: "Song 5"}
            ListElement {mediagraphic: "grey"
                songname: "Song 6"}
        }


        Component
        {
            id: grid_component

            Item {
                id: main_item
                width: grid.cellWidth;
                height: grid.cellHeight


                Rectangle {
                    id: item; parent: grid
                    color: mediagraphic
                    x: main_item.x; y: main_item.y
                    width: 100; height: 100;

                    Behavior on x { NumberAnimation { duration: 400; easing.type: Easing.OutBack } }
                    Behavior on y { NumberAnimation { duration: 400; easing.type: Easing.OutBack } }
                }
                Drag.active: mouseArea.drag.active
                Drag.hotSpot.x: main_item.width / 2
                Drag.hotSpot.y: main_item.height / 2


                MouseArea {
                    id: mouseArea
                    anchors.fill: main_item
                    drag.target: main_item

                    drag.onActiveChanged: {
                        if (mouseArea.drag.active) {
                            grid.dragItemIndex = index;
                        }
                        main_item.Drag.drop();
                    }
                }

                states: [

                    State {
                        name: 'dragged'
                        when: main_item.Drag.active
                        PropertyChanges {
                            target: main_item
                            x: x
                            y: y
                        }
                        ParentChange
                        {
                            target: main_item
                            parent: media_item
                        }
                    }
                ]

                onStateChanged: console.log('State', state)
            }
        }

    }

    Rectangle
    {
        id: now_playing_rect
        height: parent.height
        width: parent.width/2
        x: 640
        color: "Transparent"


        DropArea
        {
            id: dropArea
            width: 200
            height: 200
            anchors.centerIn: parent
            onDropped:
            {
                song_details.text = grid.model.get(grid.dragItemIndex).songname
                selected_song_image.color =  grid.model.get(grid.dragItemIndex).mediagraphic
                mediaModel.remove(grid.dragItemIndex)
            }
        }

        Rectangle {
            id: selected_song_image
            width: 200
            height: 200
            anchors.centerIn: parent
        }

        Rectangle {
            id: next
            color: 'green'
            anchors.verticalCenter: now_playing_rect.verticalCenter
            anchors.left: dropArea.right
            width: 50
            height: 50
        }

        Rectangle {
            id: previous
            color: 'brown'
            anchors.verticalCenter: now_playing_rect.verticalCenter
            anchors.right: dropArea.left
            width: 50
            height: 50
        }

        Text {
            id: song_details
            text: qsTr("Song Title")
            anchors.top: dropArea.bottom
            font.pointSize: 30
            color: "White"
            anchors.horizontalCenter: parent.horizontalCenter
        }
    }

    Item {
        id: topDrag
        anchors.fill: parent
    }
}

【问题讨论】:

    标签: qt gridview qml


    【解决方案1】:

    我编辑了您的示例。由于我没有图像,我将所有内容都更改为带颜色的矩形,以使其运行。

    为了以后的注意,如果我能从头开始运行你的示例,那就太好了。

    您正在寻找的主要变化是在各州。 通过为 x 和 y 设置“ChangeProperty”,它们将在状态变回时被重置。

    Item {
    
        id: media_item
        anchors.fill: parent
    
    
    
        Rectangle
        {
            y: 100
            id: holder_box
            width: 450
            height: 150
            color: "#99000000"
            radius: 5
    
            GridView {
                id: grid
                anchors.fill: parent
                model: mediaModel
                delegate: grid_component
                interactive: false
                anchors.topMargin: -30
    
                property int dragItemIndex: -1
    
            }
    
    
    
    
            ListModel {
                id: mediaModel
                ListElement { mediagraphic: "orchid"
                    songname: "Song 1"}
                ListElement {mediagraphic: "pink"
                    songname: "Song 2"}
                ListElement {mediagraphic: "purple"
                    songname: "Song 3"}
                ListElement {mediagraphic: "lighpink"
                    songname: "Song 4"}
                ListElement {mediagraphic: "blue"
                    songname: "Song 5"}
                ListElement {mediagraphic: "grey"
                    songname: "Song 6"}
            }
    
    
            Component
            {
                id: grid_component
    
                Item {
                    id: main_item
                    width: grid.cellWidth;
                    height: grid.cellHeight
    
    
                    Rectangle {
                        id: item; parent: grid
                        color: mediagraphic
                        x: main_item.x; y: main_item.y
                        width: 100; height: 100;
    
                        Behavior on x { NumberAnimation { duration: 400; easing.type: Easing.OutBack } }
                        Behavior on y { NumberAnimation { duration: 400; easing.type: Easing.OutBack } }
                    }
                    Drag.active: mouseArea.drag.active
                    Drag.hotSpot.x: main_item.width / 2
                    Drag.hotSpot.y: main_item.height / 2
    
    
                    MouseArea {
                        id: mouseArea
                        anchors.fill: main_item
                        drag.target: main_item
    
                        drag.onActiveChanged: {
                            if (mouseArea.drag.active) {
                                grid.dragItemIndex = index;
                            }
                            main_item.Drag.drop();
                        }
                    }
    
                    states: [
    
                        State {
                            name: 'dragged'
                            when: main_item.Drag.active
                            PropertyChanges {
                                target: main_item
                                x: x
                                y: y
                            }
                        }
                    ]
    
                    onStateChanged: console.log('State', state)
                }
            }
    
        }
    
        Rectangle
        {
            id: now_playing_rect
            height: parent.height
            width: parent.width/2
            x: 640
            color: "Transparent"
    
    
            DropArea
            {
                id: dropArea
                width: 200
                height: 200
                anchors.centerIn: parent
                onDropped:
                {
                    song_details.text = grid.model.get(grid.dragItemIndex).songname
                    selected_song_image.color =  grid.model.get(grid.dragItemIndex).mediagraphic
                    mediaModel.remove(grid.dragItemIndex)
                }
            }
    
            Rectangle {
                id: selected_song_image
                width: 200
                height: 200
                anchors.centerIn: parent
            }
    
            Rectangle {
                id: next
                color: 'green'
                anchors.verticalCenter: now_playing_rect.verticalCenter
                anchors.left: dropArea.right
                width: 50
                height: 50
            }
    
            Rectangle {
                id: previous
                color: 'brown'
                anchors.verticalCenter: now_playing_rect.verticalCenter
                anchors.right: dropArea.left
                width: 50
                height: 50
            }
    
            Text {
                id: song_details
                text: qsTr("Song Title")
                anchors.top: dropArea.bottom
                font.pointSize: 30
                color: "White"
                anchors.horizontalCenter: parent.horizontalCenter
            }
        }
    }
    

    我认为它应该按预期运行?

    请不要忘记接受我的回答,如果它是您想要的。

    【讨论】:

    • 有什么方法可以在拖动时将网格中的矩形带到所有视图的顶部..(现在如果我们拖动第一个项目,它会移动到网格中的第二个矩形下方。 .
    • 贾,有。您需要暂时将其重新设置为Item,该Item 位于其他所有内容之上。请记住,Z-Index 对于父级和子级都是本地的。
    • 在这种情况下,在您的State 中添加一个ParentChange 可能就足够了,您将在其中重新定义为media_item。但是,如果有一些 media_item 的兄弟姐妹或其任何祖先兄弟姐妹分层在 media_item 上,它仍将分层在您的 drag 上。
    • 当我添加 ParentChange { target: main_item;父:media_item;它仍然在网格项目下移动,并且当我释放拖动时,项目的 x、y 位置发生变化,破坏了网格项目对齐
    • 如果您的代码没有太大变化:a) 切换now_playing_rect 和holder_box。 b) 设置holder_box.z: 1,c) 设置now_playing_rect.z: -1
    猜你喜欢
    • 1970-01-01
    • 2014-03-11
    • 2013-04-26
    • 2023-04-04
    • 2015-05-23
    • 2016-07-26
    • 1970-01-01
    • 2016-12-30
    • 2016-10-13
    相关资源
    最近更新 更多