Step1: Enable Accept Drops APP Main Window
self.setAcceptDrops(True)
Step2 Implement Drag Event Handler
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
Step3 Implement Drop Event Handler
def dropEvent(self, event):
files = [u.toLocalFile() for u in event.mimeData().urls()]
for f in files:
print(f)
Whole code
from PyQt5.QtWidgets import QMainWindow, QApplication
import sys
class MainWidget(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Drag and Drop")
self.resize(720, 480)
'''Step1: Enable Accept Drops APP Main Window'''
self.setAcceptDrops(True)
'''Step2 Implement Drag Event Handler'''
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
'''Step3 Implement Drop Event Handler'''
def dropEvent(self, event):
files = [u.toLocalFile() for u in event.mimeData().urls()]
for f in files:
print(f)
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = MainWidget()
ui.show()
sys.exit(app.exec_())