PyQt: Getting file name for file dropped in app

TimothyAWiseman picture TimothyAWiseman · Dec 19, 2011 · Viewed 11.5k times · Source

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() + '\n\n' + e.mimeData().text()
    self.ui.fileListPTE.setPlainText(newText)

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


I couldn't quite get @ekhumoro answer to work for me, but it gave me more places to look, and I found PyQT4: 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 += '\n' + str(url.toLocalFile())
        self.ui.fileListPTE.setPlainText(newText)
        self.emit(QtCore.SIGNAL("dropped"))
    else:
        event.ignore()

Answer

ekhumoro picture ekhumoro · Dec 20, 2011

The QMimeData class has methods for dealing with dropped urls:

def dragEnterEvent(self, event):
    if event.mimeData().hasUrls():
        event.accept()
    else:
        event.ignore()

def dropEvent(self, event):
    for url in event.mimeData().urls():
        path = url.toLocalFile().toLocal8Bit().data()
        if os.path.isfile(path):
            print path
            # do other stuff with path...