Skip to content

Instantly share code, notes, and snippets.

@peace098beat
Created April 28, 2015 12:18
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save peace098beat/db8ef7161508e6500ebe to your computer and use it in GitHub Desktop.
Save peace098beat/db8ef7161508e6500ebe to your computer and use it in GitHub Desktop.
[PyQt] Drag and Drop files
class MainWidget(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWidget, self).__init__(parent)
self.setWindowTitle("FiFiFactory App")
self.resize(720,480)
self.setAcceptDrops(True)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dropEvent(self, event):
files = [unicode(u.toLocalFile()) for u in event.mimeData().urls()]
for f in files:
print f
@Teraskull
Copy link

Complete example for Python 3:

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)
        self.setAcceptDrops(True)

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

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

@aybasaran
Copy link

thank you <3

@tbrodbeck
Copy link

👍

@rundekugel
Copy link

I was looking for exactly this. Thanks!

@behrooz3500
Copy link

Simple and Useful!
Lots of thanks.

@21zz
Copy link

21zz commented Oct 28, 2022

thank you very much

@jonmatthis
Copy link

❤️ ❤️ ❤️

@Timtti
Copy link

Timtti commented Aug 27, 2023

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment