查看完整版本: [-- zhengtianzuo系列-Qml拖放 --]

QTCN开发网 -> Qt代码秀 -> zhengtianzuo系列-Qml拖放 [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

zhengtianzuo 2017-10-14 09:22

zhengtianzuo系列-Qml拖放

1. 需要在被拖放Item里设置拖放属性:

```
        Drag.active: dragArea.drag.active;//激活状态
        Drag.supportedActions: Qt.CopyAction;//设置支持的动作为拷贝
        Drag.dragType: Drag.Automatic;//拖放类型为自动
        Drag.mimeData: {"text": text};//拖放源
```

2. 设置被拖放Item的鼠标区域:

```
        MouseArea {
            id: dragArea;
            anchors.fill: parent;
            drag.target: parent;//关键是这句, 设置目标
        }
```

3.  接收端Item就比较好设置了, 增加DropArea代码:

```
            DropArea {
                id: dropContainer
                anchors.fill: parent;
                onDropped: {
                    if (drop.supportedActions == Qt.CopyAction){
                        targetEdit.text = drop.getDataAsString("text")//获取拖放的字符串并赋值
                    }
                }
            }
```

拖放字符串和图片其实是差不多的



需要完整代码请访问 QtQuickExamples

big_mouse 2020-04-22 09:13

zuoyi 2022-03-26 10:53
    


查看完整版本: [-- zhengtianzuo系列-Qml拖放 --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled