# 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:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amine](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/amine/32/4844_2.png) [@Amine](https://discourse.slicer.org/u/Amine)\
**Post date:** [December 4, 2019, 12:40pm UTC](https://discourse.slicer.org/t/add-drag-and-drop-import-to-slicelet/9382/1 "2019-12-04T12:40:12Z")

</div>

Hi, is there any way to implement the drag and drop function to a slicelet (qt widget without the main window) as it is with the the main window (hopefully other than rewriting it from scratch ) thanks

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [December 4, 2019, 2:43pm UTC](https://discourse.slicer.org/t/add-drag-and-drop-import-to-slicelet/9382/2 "2019-12-04T14:43:59Z")

</div>

Last time I checked you could only implement the drop behavior in C++ by overriding the `dropEvent` method, but maybe somebody can find a way to do it in Python.

[https://doc.qt.io/qt-5/dnd.html](https://doc.qt.io/qt-5/dnd.html)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 4, 2019, 3:15pm UTC](https://discourse.slicer.org/t/add-drag-and-drop-import-to-slicelet/9382/3 "2019-12-04T15:15:22Z")

</div>

> [@Amine](#):
>
> drag and drop function to a slicelet

We do not recommend to implement slicelets without main window for many reasons. Slicer now has built-in scripts to strip down the default main window from Python (see [here](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Slicelets)). For more control, you can create a [custom application](https://github.com/KitwareMedical/SlicerCustomAppTemplate) in C++ (based on a default main window that you can customize any way you need).

---

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

```

---

<div class="post-metadata">

**Author:** ![Amine](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/amine/32/4844_2.png) [@Amine](https://discourse.slicer.org/u/Amine)\
**Post date:** [January 20, 2020, 12:56pm UTC](https://discourse.slicer.org/t/add-drag-and-drop-import-to-slicelet/9382/5 "2020-01-20T12:56:01Z")

</div>

Thanks for your answer, i did use almost the same process without merging the filters in slicer main window and instead did an override of the qt.QWidget class methods and used it as the main slicelet window.  
Instead of `eventFilter` i had:

> ```
> class MyClass(qt.QWidget):
> def __init__ (self, type, parent=None):
> super(MyClass, self). __init__ (parent)
> self.setAcceptDrops(True)
> 
> ```

The following functions are the same,

Your approach is great to change the main slicer window drag and drop actions, thank you for pointing it out!
