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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…