- 需要在被拖放Item里設(shè)置拖放屬性:
Drag.active: dragArea.drag.active;//激活狀態(tài)
Drag.supportedActions: Qt.CopyAction;//設(shè)置支持的動(dòng)作為拷貝
Drag.dragType: Drag.Automatic;//拖放類型為自動(dòng)
Drag.mimeData: {"text": text};//拖放源
- 設(shè)置被拖放Item的鼠標(biāo)區(qū)域:
MouseArea {
id: dragArea;
anchors.fill: parent;
drag.target: parent;//關(guān)鍵是這句, 設(shè)置目標(biāo)
}
- 接收端Item就比較好設(shè)置了, 增加DropArea代碼:
DropArea {
id: dropContainer
anchors.fill: parent;
onDropped: {
if (drop.supportedActions == Qt.CopyAction){
targetEdit.text = drop.getDataAsString("text")//獲取拖放的字符串并賦值
}
}
}
拖放字符串和圖片其實(shí)是差不多的

show.gif
需要完整代碼請(qǐng)?jiān)L問(wèn)QtQuickExamples