Qt 如何通过QML中的拖放区域机制拖动项目的副本?

Qt 如何通过QML中的拖放区域机制拖动项目的副本?,qt,drag-and-drop,qml,Qt,Drag And Drop,Qml,你好, 我正在windows上为Android使用Qt5.2.1 我已经浏览了Qt提供的不同拖放示例,但没有一个解决了我的问题,虽然我发现通过拖放复制文本,但我想复制整个项目 注意:我不需要外部拷贝,只需要在应用程序内部 谢谢你我是noop,这只是一个想法 创建新对象(与拖动的项目相同) 将拖动的项目属性复制到新创建的对象中 摧毁源头 假设您有以下项目: // DraggedItem.qml import QtQuick 2.0 Item { id:draggedItem

你好, 我正在windows上为Android使用Qt5.2.1

我已经浏览了Qt提供的不同拖放示例,但没有一个解决了我的问题,虽然我发现通过拖放复制文本,但我想复制整个项目

注意:我不需要外部拷贝,只需要在应用程序内部


谢谢你

我是noop,这只是一个想法

  • 创建新对象(与拖动的项目相同)
  • 将拖动的项目属性复制到新创建的对象中
  • 摧毁源头
  • 假设您有以下项目:

        // DraggedItem.qml
    import QtQuick 2.0
    Item {
        id:draggedItem
        property string itemText
        property string imageSource
        Image {
            id: img
            source: imageSource
        }
        Text {
            id: txt
            text: qsTr("text")
        }
    }   
    
    然后,您可以将您的放置区域设置为:

    import QtQuick 2.0
    Item {
    id:dropItem
    
    DropArea {
        id: dropArea
        anchors.fill: parent
    
        onDropped: {
            if (drop.source.parent !== draggedItem) {
    
                // Create new Item
                var newItem = createNewItem();
    
                // Copy the source item properties into the new one
                newItem.width = parent.width;
                newItem.height = drop.source.height;
                newItem.itemText = drop.source.itemText
                newItem.imageSource = drop.source.imageSource
    
                // destroy the source
                drop.source.destroy();
            }
    
        }
    
    }
    
    function createNewItem() {
        var component = Qt.createComponent("DraggedItem.qml");
        if (component.status === Component.Ready)
        {
            var newItem = component.createObject(parent, {"x":0,
                                                     "y":0});
            if (newItem === null)
                console.log("Error creating new Item");
    
        }
        return newItem
      } 
    }
    

    此代码未经测试。应该有更好的方法来做到这一点

    我不明白你在这里想要实现什么。创建源对象的副本,然后销毁它,因此尽管OP请求复制,您仍然只有一个拖动的对象。这种行为与普通的QML拖动没有什么不同,只是它需要更多的代码。