QML:如何将文件从资源管理器拖动到我的Qt Quick应用程序?



我在桌面Linux(KDE(上运行QML应用程序。当文件拖放到 QML 应用程序的主窗口时,我需要以文本形式获取文件路径和文件名(带或不带前缀"file:///"(。

如果我将文件从 Dolphin 文件管理器拖到 Firefox 的地址栏,文本 URI(例如"file:///home/user/Downloads/file.txt"(会放入地址栏中,文件会在 Firefox 中打开。这表明文件的拖动信息可从操作系统获得。

我可以使用我构建的 QML 应用程序(下面提供的代码(进行的另一个测试是选择一些文本(例如在网页上(,然后将该选择拖到我的应用程序主窗口上。这有效,应用程序获取并存储文本选择包含的任何内容。

但是,我无法将文件拖放到我的 QML 应用程序上。我不明白为什么我可以拖动文本选择而不是文件。我提供了我的代码的工作示例。

在 KDE 上测试,当我将文件拖放到 QML 应用程序窗口时,调用onDropped方法。在那里我做了一个控制台.logdrop.text的(drag.source 是空的(。

应该如何实现将文件(例如,从文件管理器(拖放到 QML 应用程序?

和 QML:

import QtQuick.Window 2.2
import QtQuick 2.2
import QtQuick.Controls 1.3
import QtQuick.Layouts 1.3
import QtQuick.Dialogs 1.0
ApplicationWindow {
id: rootId
visible: true
title: "Drag Drop MRE"
property int margin: 16
minimumWidth: 600
minimumHeight: 480
FileDialog {
id: fileDialog
title: "Choose File Attachment"
folder: shortcuts.home
onAccepted: {
attachments.text += fileDialog.fileUrls + 'n'
console.log("added attachment: " + fileDialog.fileUrls)
}
}
DropArea{
id: drop
enabled: true
anchors.fill: parent
anchors.margins: margin
Rectangle {
anchors.fill: parent
color: "green"
visible: parent.containsDrag
}
onEntered: console.log("entered DropArea")
onDropped:{
attachments.text += drop.text + 'n'
console.log("added attachment: " + drop.text)
console.log("drag.source: " + drag.source)
}
Drag.dragType: Drag.Automatic
GridLayout {
id: mainLayout
anchors.fill: parent
anchors.margins: margin
Label {
text: "Drag Drop MRE"
font.pixelSize: 16
Layout.row: 1
Layout.column: 1
}
GroupBox {
Layout.row: 2
Layout.column: 1
id: gridBox
Layout.fillWidth: true
GridLayout {
id: gridLayout
rows: 1
flow: GridLayout.TopToBottom
anchors.fill: parent
Label { text: "Attached files"
}
TextArea {
id: attachments
Layout.fillWidth: true
implicitHeight: 300
}
}
}
GroupBox {
Layout.row: 3
Layout.column: 1
id: rowBox
Layout.fillWidth: true
Row {
anchors.right: parent.right
id: rowLayout
Button {
id: attach_button
text: "Attach"
onClicked: fileDialog.open();
}
Button {
id: save_button
text: "Save"
onClicked: console.log(attachments.text)
}
Button {
id: exit_button
text: "Exit"
onClicked: Qt.quit()
}
}
}
}} // Grid Layout & Drop Area
}

我发现了一个相关的问题,但情况正好相反: 将文件从应用程序拖到资源管理器。我的应用程序可以进行复制吗?

我还构建并运行了Qt示例。不幸的是,这些示例遇到了同样的问题,因此它们无助于解决问题。但是,我已经读到我正在尝试做的事情是可以做到的。我只是还没有找到可行的解决方案。

这应该可以工作(在Windows上测试(:

Window {
id: root
visible: true
width: 640
height: 480
title: qsTr("Drop Area")
DropArea {
id: dropArea;
anchors.fill: parent
onEntered: {
root.color = "gray";
drag.accept (Qt.LinkAction);
}
onDropped: {
console.log(drop.urls)
root.color = "white"
}
onExited: {
root.color = "white";
}
}
}

在Qt 6中,改用带有形式参数的JavaScript函数。

Window {
id: root
visible: true
width: 640
height: 480
title: qsTr("Drop Area")
DropArea {
id: dropArea;
anchors.fill: parent
onEntered: (drag) => {
root.color = "gray";
drag.accept (Qt.LinkAction);
}
onDropped: (drop) => {
console.log(drop.urls)
root.color = "white"
}
onExited: {
root.color = "white";
}
}
}

未声明参数"drop"。不推荐将参数注入信号处理程序。请改用带有形式参数的 JavaScript 函数。

参考: https://doc.qt.io/qt-6/qml-qtquick-droparea.html

最新更新