PyQt 将外部应用程序拖放到 Windows 文件资源管理器中? [英] PyQt drag and drop outside app into Windows file explorer?

查看:46
本文介绍了PyQt 将外部应用程序拖放到 Windows 文件资源管理器中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过从 QListWidget 拖动到操作系统文件资源管理器(在我的情况下为 Windows 10)来创建文件,这可能吗?所以在下面的小部件中,我想拖动一个"和两个"从列表到系统文件浏览器中的文件夹,以创建两个名为one.txt"的文件.和two.txt"包含文本一个"的词和两个"分别

I would like to create files by dragging from a QListWidget into the OS file explorer (Windows 10 in my case), is this possible? So in the widget below, I want dragging "one" and "two" from the list onto a folder in the system file explorer to create two files named "one.txt" and "two.txt" containing the text "one" and "two" respectively

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt


class DragTest(QtWidgets.QListWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setDragEnabled(True)
        self.addItems(['one', 'two', 'three'])
        self.setSelectionMode(self.MultiSelection)


if __name__ == "__main__":
    app = QtWidgets.QApplication.instance() or QtWidgets.QApplication(sys.argv)

    w = DragTest()
    w.show()

    app.exec_()

推荐答案

您需要实施 startDrag() 方法并将 URL 添加到 QDrag 实例.

You need to implement the startDrag() method and add the urls to the QDrag instance.

class DragTest(QtWidgets.QListWidget):
    # ...
    def startDrag(self, actions):
        drag = QtGui.QDrag(self)
        indexes = self.selectedIndexes()
        mime = self.model().mimeData(indexes)
        urlList = []
        for index in indexes:
            urlList.append(QtCore.QUrl.fromLocalFile(index.data()))
        mime.setUrls(urlList)
        drag.setMimeData(mime)
        drag.exec_(actions)

请注意,我只是使用 index.data(),因为您使用了项目名称的完整路径,但您可能更愿意为完整 url 设置特定的数据角色,以防万一,例如,您只想显示文件名:

Note that I'm just using the index.data() since you used the full path for the item name, but you might prefer to set a specific data role for the full url in case, for example, you want to show only the file name:

FullPathRole = QtCore.Qt.UserRole + 1

class DragTest(QtWidgets.QListWidget):
    # ...
    def dropEvent(self, e):
        if e.mimeData().hasUrls():
            e.setDropAction(QtCore.Qt.CopyAction)
            e.accept()
            fpath_list = []
            for url in e.mimeData().urls():
                fpath_list.append(str(url.toLocalFile()))

            for fpath in fpath_list:
                print(f'IMPORT {fpath}')
                fileName = QtCore.QFileInfo(fpath).fileName()
                item = QtWidgets.QListWidgetItem(fileName)
                item.setData(FullPathRole, fpath)
                self.addItem(fpath)

    def startDrag(self, actions):
        # ...
            urlList.append(QtCore.QUrl.fromLocalFile(index.data(FullPathRole)))

另请注意,if e.mimeData().hasUrls(): 检查中缺少括号.

Also note that you are missing the parentheses in the if e.mimeData().hasUrls(): check.

这篇关于PyQt 将外部应用程序拖放到 Windows 文件资源管理器中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆