# Handling Slicer Close Event During MorphoSourceImport Module Download

**URL:** <https://discourse.slicer.org/t/handling-slicer-close-event-during-morphosourceimport-module-download/33474>\
**Category:** Development\
**Created:** [December 20, 2023, 8:36pm UTC](https://discourse.slicer.org/t/handling-slicer-close-event-during-morphosourceimport-module-download/33474 "2023-12-20T20:36:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![oothomas](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/oothomas/32/68743_2.png) [@oothomas](https://discourse.slicer.org/u/oothomas)\
**Post date:** [December 20, 2023, 8:36pm UTC](https://discourse.slicer.org/t/handling-slicer-close-event-during-morphosourceimport-module-download/33474/1 "2023-12-20T20:36:11Z")

</div>

Hello,

I’m currently working with the MorphoSourceImport module in SlicerMorph, which utilizes a separate QProcess to download data. I’ve encountered a scenario where a user might try to close 3D Slicer while a download is still ongoing. I’m looking for advice on the best approach to intercept this close event.

Specifically, I want to prompt the user with a notification about the active download and give them the option to either proceed with closing Slicer or keep it open until the download completes. Any insights or suggestions on how to effectively implement this functionality would be greatly appreciated.

Thank you in advance for your help!

 ![InProgress Download](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/9/7927d8e3ceb0e5394e398b478a70c96f2d637dad.png)

---

<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 21, 2023, 5:39pm UTC](https://discourse.slicer.org/t/handling-slicer-close-event-during-morphosourceimport-module-download/33474/2 "2023-12-21T17:39:59Z")

</div>

You can customize the behavior of application closing as shown in [this example in the script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#override-application-close-behavior).

---

<div class="post-metadata">

**Author:** ![oothomas](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/oothomas/32/68743_2.png) [@oothomas](https://discourse.slicer.org/u/oothomas)\
**Post date:** [December 21, 2023, 6:08pm UTC](https://discourse.slicer.org/t/handling-slicer-close-event-during-morphosourceimport-module-download/33474/3 "2023-12-21T18:08:39Z")

</div>

> [@lassoan](#):
>
> this example in the script repository

Thank you! This helps a lot!

---

<div class="post-metadata">

**Author:** ![oothomas](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/oothomas/32/68743_2.png) [@oothomas](https://discourse.slicer.org/u/oothomas)\
**Post date:** [December 22, 2023, 12:25am UTC](https://discourse.slicer.org/t/handling-slicer-close-event-during-morphosourceimport-module-download/33474/4 "2023-12-22T00:25:20Z")

</div>

Here’s my event filter class:

```auto
class CloseApplicationEventFilter(qt.QWidget):
    def __init__ (self, morphoSourceImportWidget, parent=None):
        super(CloseApplicationEventFilter, self). __init__ (parent)
        self.morphoSourceImportWidget = morphoSourceImportWidget

    def eventFilter(self, object, event):
        if event.type() == qt.QEvent.Close:
            if self.morphoSourceImportWidget.downloadInProgress:
                userChoice = qt.QMessageBox.question(
                    self, "Download in Progress",
                    "A download is currently in progress. Would you like to stop the download and close the application?",
                    qt.QMessageBox.Yes | qt.QMessageBox.No, qt.QMessageBox.No
                )
                if userChoice == qt.QMessageBox.Yes:
                    self.morphoSourceImportWidget.terminateDownload()
                    event.accept()
                    return True
                else:
                    event.ignore()
                    return True
            else:
                event.accept()
                return True
        return False

```

I then add this to the setup method of my widget:

> morphoSourceImportWidget = self  
> event\_filter = CloseApplicationEventFilter(morphoSourceImportWidget)  
> slicer.util.mainWindow().installEventFilter(event\_filter)

This doesn’t seem to work so far. Am I implementing this correctly?

---

<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 22, 2023, 3:34am UTC](https://discourse.slicer.org/t/handling-slicer-close-event-during-morphosourceimport-module-download/33474/5 "2023-12-22T03:34:14Z")

</div>

It seems that you create the event filter object and store it in a local variable of the `setup` method. As soon as the `setup` method returns, all local variables, including your event filter gets deleted. Probably storing the event filter object in a member variable (`self.eventFilter = CloseApplicationEventFilter(...)`) will make it work.

---

<div class="post-metadata">

**Author:** ![oothomas](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/oothomas/32/68743_2.png) [@oothomas](https://discourse.slicer.org/u/oothomas)\
**Post date:** [December 22, 2023, 3:47am UTC](https://discourse.slicer.org/t/handling-slicer-close-event-during-morphosourceimport-module-download/33474/6 "2023-12-22T03:47:07Z")

</div>

Ah. Rookie mistake, lol. Thank you. Worked like a charm!
