Repository: napari/napari

Issue:
Deletion of layers gives error after canceled quitting
### 🐛 Bug Report

After a canceled quit of napari, layers cannot be deleted and lead to the following error:

...
~\Miniconda3\envs\py3\lib\site-packages\in_n_out\_store.py in _exec(*args=(), **kwargs={})
    825                             )
    826
--> 827                     raise TypeError(
        global TypeError = undefined
        e = undefined
    828                         f"After injecting dependencies for {_argnames}, {e}"
    829                     ) from e

TypeError: After injecting dependencies for NO arguments, delete_selected_layers() missing 1 required positional argument: 'viewer'

### 💡 Steps to Reproduce

1. Open Napari
2. Load an image
3. Click on window close button
4. Cancel closing in the dialog
5. Delete layer --> error


### 💡 Expected Behavior

All napari functions should work normally after canceled closing.

### 🌎 Environment

napari: 0.4.18
Platform: Windows-10-10.0.19045-SP0
Python: 3.10.2 | packaged by conda-forge | (main, Feb  1 2022, 19:26:10) [MSC v.1929 64 bit (AMD64)]
Qt: 5.12.9
PyQt5: 5.12.3
NumPy: 1.22.2
SciPy: 1.8.0
Dask: 2023.7.0
VisPy: 0.12.2
magicgui: 0.7.2
superqt: unknown
in-n-out: 0.1.8
app-model: 0.1.4
npe2: 0.7.0

OpenGL:
  - GL version:  4.6.0 - Build 26.20.100.8142
  - MAX_TEXTURE_SIZE: 16384

Screens:
  - screen 1: resolution 2560x1440, scale 1.0

Settings path:
  - ...AppData\Local\napari\py3_89ac16185f500589e64f725e503bec3c1330e4bb\settings.yaml
Plugins:
  - napari: 0.4.18 (77 contributions)
  - napari-console: 0.0.8 (0 contributions)
  - napari-crop: 0.1.9 (4 contributions)
  - napari-spero: 0.0.1 (3 contributions)
  - napari-svg: 0.1.10 (2 contributions)
  - napari-tools-menu: 0.1.19 (0 contributions)
  - napari-z-plotter: 0.0.6 (2 contributions)

### 💡 Additional Context

_No response_


Discussion:

Resolving patch:
diff --git a/napari/_qt/qt_main_window.py b/napari/_qt/qt_main_window.py
--- a/napari/_qt/qt_main_window.py
+++ b/napari/_qt/qt_main_window.py
@@ -219,17 +219,20 @@ def event(self, e: QEvent) -> bool:
                 else e.globalPos()
             )
             QToolTip.showText(pnt, self._qt_viewer.viewer.tooltip.text, self)
-        if e.type() == QEvent.Type.Close:
-            # when we close the MainWindow, remove it from the instance list
-            with contextlib.suppress(ValueError):
-                _QtMainWindow._instances.remove(self)
         if e.type() in {QEvent.Type.WindowActivate, QEvent.Type.ZOrderChange}:
             # upon activation or raise_, put window at the end of _instances
             with contextlib.suppress(ValueError):
                 inst = _QtMainWindow._instances
                 inst.append(inst.pop(inst.index(self)))
 
-        return super().event(e)
+        res = super().event(e)
+
+        if e.type() == QEvent.Type.Close and e.isAccepted():
+            # when we close the MainWindow, remove it from the instance list
+            with contextlib.suppress(ValueError):
+                _QtMainWindow._instances.remove(self)
+
+        return res
 
     def isFullScreen(self):
         # Needed to prevent errors when going to fullscreen mode on Windows


Test patch: