查看项目被拖放到 qml 中的拖放区域之外

2023-12-09

我实现了一个 Gridview,它有不同的图像,我需要将项目拖出网格视图并将其放置在 DropArea 中。我跟着如何在 QML 中将项目拖动到 ListView 之外但现在我的网格项可以拖动到整个页面的任何位置。我需要将其限制为 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
    }
}

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

为了将来的说明,如果我能从头开始运行你的示例,那就太好了。

您正在寻找的主要变化是在各州。 通过为 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
        }
    }
}

我认为它应该像预期的那样表现?

如果这是您正在寻找的内容,请不要忘记接受我的答案。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

查看项目被拖放到 qml 中的拖放区域之外 的相关文章

  • 为什么动态 qml 对象的创建如此缓慢,有哪些合理的替代方案?

    我想要实现的目标类似于棋盘游戏 有一个100 100的网格 放在一个Item它驻留在一个Flickable 游戏板 的各个矩形都是 svg 图像 目前大约有 20 种 可能会增加到数百种 作为基准测试 我只是尝试用元素填充 世界 Compo
  • 屏幕滚动时 GridView 内的项目会重复

    我使用 GridView 来显示一组用户可以选择的类别 网格的每个项目都由一个 ImageView 和一个 TextView 组成 两者都是从服务器检索的 当触摸一个项目时 另一个活动就会启动 我以为一切都很顺利 直到我注意到当我滚动屏幕时
  • QThread - 使用槽 quit() 退出线程

    我想在线程完成运行时通知对象 但是 我无法让线程正确退出 我有以下代码 处理器 cpp thread new QThread tw new ThreadWorker connect tw SIGNAL updateStatus QStrin
  • QML MouseArea 将事件传播到按钮

    我正在开发一个应用程序 其菜单类似于 Android 版 Gmail 收件箱应用程序菜单 基本上 当您按下按钮打开菜单时 它就会滑入视图 用户可以将其滑开或按菜单上的按钮 对于滑动我使用了代码SwipeArea from kovrov ht
  • 相对文件路径的区别:Qt Creator的调试模式和发布模式

    QFile file test txt if file open QIODevice ReadOnly qDebug lt lt You got me 我在用 Qt 4 8 6 与 MSVC 2010 Qt 创建者 3 1 1 Window
  • ASP.NET 更改模板字段中 Gridview 单元格中的文本和颜色

    我在 ASP net 中有 Gridview 显示数据 根据数据 它会根据单元格的值更改颜色和文本 当列不是模板字段时 这可以正常工作 WORKS WHEN IS NOT A TEMPLATE FIELD if e Row RowType
  • Android 初学者:Android gridview 中的触摸事件

    我正在使用以下代码来使用 gridview 执行操作 稍作修改http developer android com resources tutorials views hello gridview html http developer a
  • qdbusxml2cpp 未知类型

    在使用 qdbusxml2cpp 程序将以下 xml 转换为 Qt 类时 我收到此错误 qdbusxml2cpp c ObjectManager a ObjectManager ObjectManager cpp xml object ma
  • 如何安装 C++ 的 VOCE?

    我正在尝试安装 VOCE api 它是为 C 和 Java 构建的语音识别 API 这是我第二次使用外部 C 库 也是第一次使用 Java C api 语音链接 http voce sourceforge net http voce sou
  • Android 上与 Qt 5.2 的蓝牙通信[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我想使用 Qt 框架使我的 Android 设备能够通过蓝牙进行通信 截至今天 Qt 5 2 是最新的 据我所知 尚不支持蓝牙 在本页
  • QTextEdit 与 QPlainTextEdit

    有什么区别QTextEdit and QPlainTextEdit 为什么要使用其中一种而不是另一种 我正在编写一个文本编辑器作为学习 Qt5 的练习 现在我想知道是否使用QTextEdit or QPlainTextEdit 到目前为止我
  • 通过信号/槽将 QVector 从工作线程传递到主线程

    目前 我在将 QVector 传递到线程之间时遇到一些麻烦 目前我有一个主线程 GUI Thread 和一个频繁发出 QVector 数组的工作线程 在向量内直接发出数据之前看起来不错 接收者是主线程中的一个槽 但该槽接收到的数据是乱码 这
  • 从列表视图传递到网格视图

    我有一个带有列表的活动 其项目由图像 文本组成 我需要允许用户更改视图并使用 gridview 代替它 其元素仍然由相同的图像 文本组成 用户可以通过图标菜单来完成此操作 public boolean onOptionsItemSelect
  • Qt:使用 QObject::connect 指定多种连接类型

    我想知道是否可以指定多种连接类型 例如 我希望我的连接类型是排队连接和唯一连接 是否可以在一份声明中具体说明这一点 QObject connect ptrSender SIGNAL ptrReceiver SLOT Queued and u
  • 如何使用 Qt/C++ 创建/读取/写入文件并将设置存储在程序本地

    我是一个不幸的 C 初学者 使用 Qt GUI 设计器程序似乎非常适合我的需求 但我在尝试编写所需的代码时遇到了问题 我可以使用 QSettings 字符串在硬盘驱动器上存储本地设置 但我个人讨厌程序执行某些程序所做的 HOME LOCAL
  • 如何从 GridView 的适配器获取每个 EditText 的值?

    我正在开发一个矩阵计算器应用程序 但我无法弄清楚如何获取 GridView 中每个 EditText 的值 我需要获取这些值以便将它们放入另一个矩阵中并计算它 如果用户想要使用 3x2 矩阵 则 GridView 的外观如下 这是一个包含六
  • 从 QTableView 读取和写入文件

    如何读取和写入输入 QTableView 的文本文件日期 这就是我所拥有的 但我想在将数据添加到表中时保存数据 当然能够在重新打开应用程序时读回它 有什么教程可以参考吗 MainWindow MainWindow QWidget paren
  • QSettings - ini 文件的位置在哪里?

    我在用着QSettings在 Windows 中将一些数据存储为 ini 文件 我想查看ini文件 但我不知道ini文件的位置在哪里 这是我的代码 QSettings set new QSettings QSettings IniForma
  • Qt - 如何在保留选中状态的同时禁用 QCheckBox?

    我有一个带有两个复选框的对话框 我们称它们为 A 和 B 当未选中 A 时 B 应该能够根据用户的需要进行切换 当 A 被选中时 B 不应该能够被切换 现在 我的对话框构造函数中有以下内容 connect ui gt A SIGNAL to
  • 防止 QGraphicsItem 移出 QGraphicsScene

    我有一个场景 其固定尺寸从 0 0 到 481 270 scene gt setSceneRect 0 0 481 270 在里面 我有一个习惯GraphicsItem多亏了旗帜我可以移动它ItemisMovable 但我希望它留在场景中

随机推荐