# Add Drag and drop import to slicelet?

**URL:** <https://discourse.slicer.org/t/add-drag-and-drop-import-to-slicelet/9382>\
**Category:** Development\
**Created:** [December 4, 2019, 12:40pm UTC](https://discourse.slicer.org/t/add-drag-and-drop-import-to-slicelet/9382 "2019-12-04T12:40:12Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [January 19, 2020, 6:28pm UTC](https://discourse.slicer.org/t/add-drag-and-drop-import-to-slicelet/9382/4 "2020-01-19T18:28:02Z")

</div>

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.

```python
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)

```

---

_[View the full topic](https://discourse.slicer.org/t/add-drag-and-drop-import-to-slicelet/9382)._
