示範效果

//文本拖放測試
Text{
id: sourceLabel
anchors.left: parent.left
anchors.leftMargin: 120
anchors.verticalCenter: parent.verticalCenter
text: qsTr("文字内容拖放測試")
Drag.active: dragArea.drag.active;//拖放激活
Drag.supportedActions: Qt.CopyAction;//拖放動作-複制内容
Drag.dragType: Drag.Automatic;//拖放類型
Drag.mimeData: {"text": text};//拖放資料類型-文本
//滑鼠區域
MouseArea {
id: dragArea;
anchors.fill: parent;
drag.target: parent;
}
}
TextEdit{
id: targetEdit
height: parent.height-2
width: parent.width-2
anchors.centerIn: parent
text: qsTr("")
//拖放區域
DropArea {
id: dropContainer
anchors.fill: parent;
//接受拖放處理
onDropped: {
if (drop.supportedActions == Qt.CopyAction){
targetEdit.text = drop.getDataAsString("text") //指派拖放文本給新文本對象
}
}
}
}
import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
ApplicationWindow {
visible: true
width: 400
height: 300
title: qsTr("Qt基于Qml拖放功能實作")
//文本拖放測試
Text{
id: sourceLabel
anchors.left: parent.left
anchors.leftMargin: 120
anchors.verticalCenter: parent.verticalCenter
text: qsTr("文字内容拖放測試")
Drag.active: dragArea.drag.active;//拖放激活
Drag.supportedActions: Qt.CopyAction;//拖放動作-複制内容
Drag.dragType: Drag.Automatic;//拖放類型
Drag.mimeData: {"text": text};//拖放資料類型-文本
//滑鼠區域
MouseArea {
id: dragArea;
anchors.fill: parent;
drag.target: parent;
}
}
Rectangle{
anchors.left: sourceLabel.left
anchors.top: sourceLabel.bottom
anchors.topMargin: 20
border.color: "#AAAAAA"
border.width: 1
height: 25
width: 100
TextEdit{
id: targetEdit
height: parent.height-2
width: parent.width-2
anchors.centerIn: parent
text: qsTr("")
//拖放區域
DropArea {
id: dropContainer
anchors.fill: parent;
//接受拖放處理
onDropped: {
if (drop.supportedActions == Qt.CopyAction){
targetEdit.text = drop.getDataAsString("text") //指派拖放文本給新文本對象
}
}
}
}
}
//圖像拖放測試
Image{
id: sourceImage
height: 36
width: 36
anchors.right: parent.right
anchors.rightMargin: 120
anchors.verticalCenter: parent.verticalCenter
source: "qrc:/Face.png"
Drag.active: dragArea1.drag.active;//拖放激活
Drag.supportedActions: Qt.CopyAction;//拖放動作-複制内容
Drag.dragType: Drag.Automatic;//拖放類型
Drag.mimeData: {"pic": source};//拖放資料類型-圖像
//滑鼠區域
MouseArea {
id: dragArea1;
anchors.fill: parent;
drag.target: parent;
}
}
Rectangle{
anchors.left: sourceImage.left
anchors.top: sourceLabel.bottom
anchors.topMargin: 20
border.color: "#AAAAAA"
border.width: 1
height: 36
width: 36
Image{
id: targetImage
height: parent.height-2
width: parent.width-2
anchors.centerIn: parent
//拖放區域
DropArea {
id: dropContainer1
anchors.fill: parent;
//接受拖放處理
onDropped: {
if (drop.supportedActions == Qt.CopyAction){
targetImage.source = drop.getDataAsString("pic") //指派拖放圖像路徑給新圖像對象
}
}
}
}
}
}