如何在 PyQt/PySide 中获取删除的文件名

时间:2023-04-28
本文介绍了如何在 PyQt/PySide 中获取删除的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试设置一个应用程序,该应用程序将接受放入其中的 havin 文件.所以,我正在寻找一种方法来在它们被放入时提取路径.

I am trying to set up an application that will accept havin files dropped into it. So, I am looking for a way to extract the path when they are dropped in.

现在,我为应用程序的右侧启用了拖放功能,它会接受放入的文本,但我不知道如何处理放入的文件.

Right now, I have drag and drop enabled for the right part of the application, and it will accept text dropped in, but I do not know how to handle having a file dropped in.

我正在使用:

def PTE_dragEnterEvent(self, e):
    if e.mimeData().hasFormat('text/plain'):
        e.accept()
    else:
        e.ignore() 

def PTE_dropEvent(self, e):
    newText = self.ui.fileListPTE.toPlainText() + '

' + e.mimeData().text()
    self.ui.fileListPTE.setPlainText(newText)

稍微修改了Zetcode 拖放教程中提供的代码.

Which is slightly modifying the code provided in the Zetcode Drag and Drop tutorial.

我无法完全让 @ekhumoro 回答为我工作,但它给了我更多可以查看的地方,我发现 Drag并将文件放入 QListWidget 这有帮助.

I couldn't quite get @ekhumoro answer to work for me, but it gave me more places to look, and I found Drag and drop files into QListWidget which helped.

除了 ekhumoro 提出的建议之外,我还需要实现拖动移动事件.我最终使用的样子:

In addition to the suggestions made by ekhumoro I needed to implement the drag move event. What I finally used looked like:

def dragEnterEvent(self, event):
    if event.mimeData().hasUrls:
        event.accept()
    else:
        event.ignore()
        
def dragMoveEvent(self, event):
    if event.mimeData().hasUrls:
        event.setDropAction(QtCore.Qt.CopyAction)
        event.accept()
    else:
        event.ignore()

def dropEvent(self, event):
    if event.mimeData().hasUrls:
        event.setDropAction(QtCore.Qt.CopyAction)
        event.accept()
        
        newText = self.ui.fileListPTE.toPlainText()
        for url in event.mimeData().urls():
            newText += '
' + str(url.toLocalFile())
        self.ui.fileListPTE.setPlainText(newText)
        self.emit(QtCore.SIGNAL("dropped"))
    else:
        event.ignore()

推荐答案

QMimeData 类具有处理丢弃 url 的方法.下面是一个最小的工作示例:

The QMimeData class has methods for dealing with dropped urls. Below is a minimal working example:

# from PyQt4.QtGui import QApplication, QLabel
# from PySide2.QtWidgets import QApplication, QLabel
from PyQt5.QtWidgets import QApplication, QLabel

class Window(QLabel):
    def __init__(self):
        super().__init__()
        self.setAcceptDrops(True)

    def dragEnterEvent(self, event):
        print('drag-enter')
        if event.mimeData().hasUrls():
            print('has urls')
            event.accept()
        else:
            event.ignore()

    def dropEvent(self, event):
        lines = []
        for url in event.mimeData().urls():
            lines.append('dropped: %r' % url.toLocalFile())
        self.setText('
'.join(lines))
    
app = QApplication(['Drag & Drop'])
window = Window()
window.setGeometry(50, 100, 400, 300)
window.show()
app.exec_()

更新:

关于问题的补充:

一些小部件(与上面使用的 QLabel 不同)有一个默认的 dragMoveEvent 实现,它明确地忽略大多数事件.例如,基于 QAbstractItemView 的类可能只处理某些类型的内部移动而忽略其他所有内容.在这种情况下,应添加重新实现 dragMoveEvent 以明确接受需要以不同方式处理的事件:

Some widgets (unlike the QLabel used above) have a default implementation of dragMoveEvent that explicitly ignores most events. For example, classes based on QAbstractItemView may only handle certain kinds of internal move and ignore everything else. In which case, a reimplementation of dragMoveEvent should be added that explicitly accepts the events that need to be handled differently:

class MyView(QTableView):
    ...
    def dragMoveEvent(self, event):
        if event.mimeData().hasUrls():
            event.accept()
        else:
            super().dragMoveEvent(event)

这篇关于如何在 PyQt/PySide 中获取删除的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:在 Windows 资源管理器中拖放到 Python 脚本 下一篇:在 QgraphicsView 内拖放不起作用(PyQt)

相关文章

最新文章