Search code examples
pythonqtpyqtpyside2

PySide2 drag & drop functionality does not work


I have this simple script which uses PySide2 although I tried the same script with PyQt5 with the same result. I am trying to drag & drop files onto my window and get their file path:

import sys
from PySide2.QtWidgets import QApplication, QMainWindow


class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setAcceptDrops(True)

    def dragEnterEvent(self, e):
        if e.mimeData().hasUrls():
            e.acceptProposedAction()

    def dropEvent(self, e):
        for url in e.mimeData().urls():
            file_name = url.toLocalFile()
            print("Dropped file: " + file_name)


app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()

On my system this does not allow drag & drop actions to this window. dragEnterEvent is never called. Am I missing something?


Solution

  • It turns out that it happens on some systems with Windows 10. The solution is to disable EnableLUA from registry:

    HKEY_LOCAL_MACHINE > SOFTWARE > Microsoft > Windows > CurrentVersion > Policies > System

    Change the key EnableLUA from 1 to 0. Then restart your computer. Note that this will cause your system to not show any dialog if a program tries to change something on your system which might be a security issue.