Repository: napari/napari

Issue:
Set objectName on all QDockWidgets to avoid warning at shutdown
## 🐛 Bug
I've been seeing this warning a lot when closing napari:

```
WARNING: QMainWindow::saveState(): 'objectName' not set for QDockWidget 0x7f8cf9467330 ';
```

the docs on [saveState](https://doc.qt.io/qt-5/qmainwindow.html#saveState) say:

> The objectName property is used to identify each QToolBar and QDockWidget. You should make sure that this property is unique for each QToolBar and QDockWidget you add to the QMainWindow

so we should probably make sure to add some sort of objectName to each of our QDockWidgets in 

https://github.com/napari/napari/blob/c618f310a5c837e77799f2d80286eea1db214f57/napari/_qt/widgets/qt_viewer_dock_widget.py#L21


Discussion:
Yes, I've seen that too!! Agreed
@tlambert03 is this related to plugin widgets? I have not seen this 🤔 
Probably. I don’t see it every time either... but if you just start using viewer.window.add_dock_widget you’ll probably hit it.



> On Apr 22, 2021, at 8:41 PM, Gonzalo Peña-Castellanos ***@***.***> wrote:
> 
> ﻿
> @tlambert03 is this related to plugin widgets? I have not seen this 🤔
> 
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub, or unsubscribe.

```python

    def add_dock_widget(
        self,
        widget: QWidget,
        *,
        name: str = '',
        area: str = 'bottom',
        allowed_areas=None,
        shortcut=None,
    ):
```

why is name optional 🤔 ? Should we add some uuid in case name is empty?

Name will actually be shown in the title bar, so while creating a uuid for objectName does sound like a good idea, we want to make sure it doesn’t show up in the title bar 
Then a generic `Dock widget 1`  `Dock widget 2` etc?
yeah, that would be fine.  We could maybe check to see if the inner widget being wrapped by the DockWidget has an objectName... but it probably wont :) so anything that satisfies `window.getState` is fine by me :)

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
@@ -278,6 +278,8 @@ def __init__(self, viewer, *, show: bool = True):
         # create QApplication if it doesn't already exist
         get_app()
 
+        self._unnamed_dockwidget_count = 1
+
         # Connect the Viewer and create the Main Window
         self._qt_window = _QtMainWindow()
         self.qt_viewer = QtViewer(viewer, show_welcome_screen=True)
@@ -876,7 +878,10 @@ def add_dock_widget(
         allowed_areas=None,
         shortcut=None,
     ):
-        """Convenience method to add a QDockWidget to the main window
+        """Convenience method to add a QDockWidget to the main window.
+
+        If name is not provided a generic name will be addded to avoid
+        `saveState` warnings on close.
 
         Parameters
         ----------
@@ -899,6 +904,16 @@ def add_dock_widget(
         dock_widget : QtViewerDockWidget
             `dock_widget` that can pass viewer events.
         """
+        if not name:
+            try:
+                name = widget.objectName()
+            except AttributeError:
+                name = trans._(
+                    "Dock widget {number}",
+                    number=self._unnamed_dockwidget_count,
+                )
+
+            self._unnamed_dockwidget_count += 1
 
         dock_widget = QtViewerDockWidget(
             self.qt_viewer,


Test patch: