# Bypassing the save dialog box on exit from a ScriptedLoadableModule

**URL:** https://discourse.slicer.org/t/bypassing-the-save-dialog-box-on-exit-from-a-scriptedloadablemodule/15030
**Category:** Development
**Created:** [December 12, 2020, 9:54pm UTC](https://discourse.slicer.org/t/bypassing-the-save-dialog-box-on-exit-from-a-scriptedloadablemodule/15030 "2020-12-12T21:54:58Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![hina-shah](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hina-shah/32/695_2.png) [@hina-shah](https://discourse.slicer.org/u/hina-shah)
#### Post date: [December 12, 2020, 9:54pm UTC](https://discourse.slicer.org/t/bypassing-the-save-dialog-box-on-exit-from-a-scriptedloadablemodule/15030/1 "2020-12-12T21:54:58Z")

</div>

This might be trivial, but I couldn’t find a solution. So am reaching out.

I am writing a small extension that is supposed to help with labeling some images and segmentation editing in Python. The extension loads the data, and saves its own files on exit. The example at [QuickSegment](https://github.com/lassoan/SlicerSimpleWorkflows/blob/master/QuickSegment) helped me to remove all the unnecessary Slicer details that I do not want. However, when I exit the Slicer window, it pops up the dialog box for confirmation on the scene modifications and hence saving the changes. Is it possible to not pop that dialog up? What would be the corresponding event?

I’m handling the relevant data export on the widget’s exit (i.e. `exit()`), so I don’t need that dialog to pop up again and confuse my users.

Thank you!

---

<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: [December 13, 2020, 12:27am UTC](https://discourse.slicer.org/t/bypassing-the-save-dialog-box-on-exit-from-a-scriptedloadablemodule/15030/2 "2020-12-13T00:27:18Z")

</div>

Hi Hina!

I did some work to override the Slicer drag and drop behavior to do something different that I posted about at

> [@Add Drag and drop import to slicelet?](https://discourse.slicer.org/t/add-drag-and-drop-import-to-slicelet/9382/4):
>
> 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. class MyClass(qt.QWidget): def eventFilter(self, object, event): """ Custom event filter for Slicer Main Window. Inputs: Object (QObject), Event (QEvent) """ …

You should be able to use this method for overriding the QMainWindow `closeEvent` to do your own set of actions instead of the Slicer unsaved changes warning.

Here is some [example code](https://github.com/Slicer/Slicer/blob/39fe1598d1fa2e5dcf51a712cdcdcfe7c0ce446e/Modules/Scripted/DICOM/DICOM.py#L479-L525) that @lassoan then used in the DICOM module to override the regular Slicer drag and drop event behavior.

---

<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 13, 2020, 12:49am UTC](https://discourse.slicer.org/t/bypassing-the-save-dialog-box-on-exit-from-a-scriptedloadablemodule/15030/3 "2020-12-13T00:49:32Z")

</div>

Excellent idea @jamesobutler! I’ve added an example for this [here](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Override_application_close_behavior).

Note that there is also a solution to override the default save scene behavior (e.g., save/export data automatically, without asking anything from the user), as shown [here](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Override_default_scene_save_dialog).

---

<div class="post-metadata">

### Author: ![hina-shah](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hina-shah/32/695_2.png) [@hina-shah](https://discourse.slicer.org/u/hina-shah)
#### Post date: [December 14, 2020, 2:15pm UTC](https://discourse.slicer.org/t/bypassing-the-save-dialog-box-on-exit-from-a-scriptedloadablemodule/15030/4 "2020-12-14T14:15:27Z")

</div>

Hi James!  
Thanks for the idea! I’ll need to dig into it a little bit deeper, but right now I just modified the behavior of the default dialog box like @lassoan suggested.

Hope you’re doing well!

---

<div class="post-metadata">

### Author: ![hina-shah](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hina-shah/32/695_2.png) [@hina-shah](https://discourse.slicer.org/u/hina-shah)
#### Post date: [December 14, 2020, 2:16pm UTC](https://discourse.slicer.org/t/bypassing-the-save-dialog-box-on-exit-from-a-scriptedloadablemodule/15030/5 "2020-12-14T14:16:54Z")

</div>

Thanks!

I think to completely bypass that dialog, the event trigger should be used. For me just overriding the default save scene behavior works for now!

Thank you for your suggestion!

---

<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 14, 2020, 2:40pm UTC](https://discourse.slicer.org/t/bypassing-the-save-dialog-box-on-exit-from-a-scriptedloadablemodule/15030/6 "2020-12-14T14:40:08Z")

</div>

I gave complete examples for both customizing the application close behavior (using event filter) and creating a custom save dialog (by adding a new file dialog class). For an optimal user experience probably you want to customize both.

---

<div class="post-metadata">

### Author: ![fbordignon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fbordignon/32/5269_2.png) [@fbordignon](https://discourse.slicer.org/u/fbordignon)
#### Post date: [September 28, 2021, 5:38pm UTC](https://discourse.slicer.org/t/bypassing-the-save-dialog-box-on-exit-from-a-scriptedloadablemodule/15030/7 "2021-09-28T17:38:14Z")

</div>

Updated links to the script repository  
1 - [Script repository — 3D Slicer documentation](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#override-application-close-behavior)

2- [Script repository — 3D Slicer documentation](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#override-default-scene-save-dialog)

Thanks for the snippets Andras!

---

<div class="post-metadata">

### Author: ![fbordignon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fbordignon/32/5269_2.png) [@fbordignon](https://discourse.slicer.org/u/fbordignon)
#### Post date: [December 2, 2021, 5:58pm UTC](https://discourse.slicer.org/t/bypassing-the-save-dialog-box-on-exit-from-a-scriptedloadablemodule/15030/8 "2021-12-02T17:58:07Z")

</div>

I’ve encountered a side effect of filtering the close event, as there are operations that Slicer performs on close that if we accept the event and return True will not be performed such as saving the window geometry when “Save user interface size and position on exit” option on Settings\>Appearance.  
If the user saves the scene, there is no problem returning False, because Slicer will close as it detects that the scene is not modified. If the user cancel exiting, it is ok also, because we can reject. But when closing without saving, the user wants to close but the developer needs to allow Slicer to perform its on close events minus opening the close dialog again.

---

<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 2, 2021, 6:21pm UTC](https://discourse.slicer.org/t/bypassing-the-save-dialog-box-on-exit-from-a-scriptedloadablemodule/15030/9 "2021-12-02T18:21:29Z")

</div>

We have recently exposed `slicer.util.mainWindow().saveGUIState()` method to support this use case. Is there anything else that you would like to do in your custom exit handler?

---

<div class="post-metadata">

### Author: ![fbordignon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fbordignon/32/5269_2.png) [@fbordignon](https://discourse.slicer.org/u/fbordignon)
#### Post date: [December 2, 2021, 6:42pm UTC](https://discourse.slicer.org/t/bypassing-the-save-dialog-box-on-exit-from-a-scriptedloadablemodule/15030/10 "2021-12-02T18:42:28Z")

</div>

Great, that would solve for us. Thanks!

Would checking the event state on the [closeEvent method](https://github.com/Slicer/Slicer/blob/d7870308744af4d98100c26712afa70b3baa78d3/Base/QTApp/qSlicerMainWindow.cxx#L1100) like this is desirable?

` if (event->isAccepted() || d->confirmCloseApplication())`

This way if the close event is already accepted, slicer performs the normal on close routines.

---

<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 2, 2021, 6:51pm UTC](https://discourse.slicer.org/t/bypassing-the-save-dialog-box-on-exit-from-a-scriptedloadablemodule/15030/11 "2021-12-02T18:51:46Z")

</div>

I haven’t checked the code but based on what you describe it sounds like setting the accepted state could be an acceptable solution.
