Bypassing the save dialog box on exit from a ScriptedLoadableModule

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

Hi Hina!

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

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 that @lassoan then used in the DICOM module to override the regular Slicer drag and drop event behavior.

Excellent idea @jamesobutler! I’ve added an example for this here.

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.

1 Like

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!

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!

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.

Updated links to the script repository
1 - Script repository — 3D Slicer documentation

2- Script repository — 3D Slicer documentation

Thanks for the snippets Andras!

2 Likes

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.

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?

1 Like

Great, that would solve for us. Thanks!

Would checking the event state on the closeEvent method 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.

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