I came across this thread as I was experimenting adding a custom drop action for a custom app using only python. With the code below I was able to redefine the drop action to execute what I put in dropEvent
when I dropped a file path instead of the Slicer default of opening the Add Data Dialog.
class MyClass(qt.QWidget):
def eventFilter(self, object, event):
"""
Custom event filter for Slicer Main Window.
Inputs: Object (QObject), Event (QEvent)
"""
if event.type() == qt.QEvent.DragEnter:
self.dragEnterEvent(event)
return True
if event.type() == qt.QEvent.Drop:
self.dropEvent(event)
return True
return False
def dragEnterEvent(self, event):
"""
Actions to do when a drag enter event occurs in the Main Window.
Read up on https://doc.qt.io/qt-5.12/dnd.html#dropping
Input: Event (QEvent)
"""
if event.mimeData().hasUrls:
event.acceptProposedAction() # allows drop event to proceed
else:
event.ignore()
def dropEvent(self, event):
"""
Actions to do when an item is dropped onto the Main Window.
Read up on https://doc.qt.io/qt-5.12/dnd.html#dropping
Input: Event (QEvent)
"""
print("Custom drop code goes here")
my_class = MyClass()
slicer.util.mainWindow().installEventFilter(my_class)