3

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?

Canol Gökel
  • 1,168
  • 2
  • 13
  • 29
  • Your script works as expected (allows drag & drop and prints file path) on my computer with python 3.6.5 on Windows 7, tested with PySide2 5.11.2 and 5.12.0. What is your configuration? – jpeg Jan 29 '19 at 13:54
  • @jpeg Thanks for trying it out, my configuration is Windows 10 (1809), Python 3.6.8, PySide2 5.12. I also tried with PySide2 5.11.2. It might be a Windows 10 issue maybe? My colleague also tried it on his computer with the same results. – Canol Gökel Jan 29 '19 at 14:24
  • Does drag and drop actually work for other applications at all on your system? A web search with for example `Drag and drop not working in windows 10` yields plenty of resources, one of them might help solving your problem...? – jpeg Jan 29 '19 at 14:50
  • @jpeg Thanks, yes one of the solutions worked: disabling EnableLUA using regedit. But I don't understand why this is needed for PySide2 applications because drag & drop works on my other applications even when EnableLUA is enabled, for example I can open files in Sublime by just dragging without a problem. – Canol Gökel Jan 29 '19 at 15:42
  • Sounds great. Please consider adding your solution as answer and marking it as accepted. I don't understand either why it is needed for PySide2 under Windows 10 but not under Windows 7, one should probably ask Microsoft... – jpeg Jan 29 '19 at 15:49

1 Answers1

4

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.

Canol Gökel
  • 1,168
  • 2
  • 13
  • 29