Handling Slicer Close Event During MorphoSourceImport Module Download

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!

You can customize the behavior of application closing as shown in this example in the script repository.

Thank you! This helps a lot!

Here’s my event filter class:

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?

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.

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

1 Like