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