Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

python - How to get dropped file names in PyQt/PySide

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.

I am using:

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)

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


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.

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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_()

UPDATE:

Regarding the additions to the question:

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...