Napari: consider orphaning dock_widget children before removing dock widgets

Created on 22 Dec 2020  路  3Comments  路  Source: napari/napari

I'm _finally_ understanding an issue @stefanv posted a long time ago on magicgui (https://github.com/napari/magicgui/issues/28). I think it _may_ be something that we could/should fix in our dock widget mechanisms. Basically, when we call viewer.window.add_dock_widget we assign the new QDockWidget as the parent of whatever widget was passed in:
https://github.com/napari/napari/blob/ddd6715208eb02774d01c49444d45764b42b5e25/napari/_qt/widgets/qt_viewer_dock_widget.py#L100-L101

however, if that QDockWidget gets removed from the viewer, _and_ the user hasn't saved a reference somewhere else to the QDockWidget, then the original widget that was passed to add_dock_widget will get deleted, _even if they have a reference to it somewhere else_, because a QWidget cleans up all of its children when it gets destroyed. One possible fix is to call widget._widget.setParent(None) right here before we remove the dock widget:
https://github.com/napari/napari/blob/ddd6715208eb02774d01c49444d45764b42b5e25/napari/_qt/qt_main_window.py#L612
(where widget was QDockWidget instance, and widget._widget was the original widget passed by the user)

bug

All 3 comments

it's also possible that we shouldn't be calling setParent on the original widget in the first place (but if we don't, I'm not sure that styles will be passed on to the child)

Nice! Your proposed solution sounds reasonable to me.

Sounds reasonable to me too.

One other thing I noted in #1800 was I think we also need to remove the toggle view action https://github.com/napari/napari/pull/1800/files#diff-a668967bb40dcd4fe528babf5641ceb5f8c2bafb6b6e57e5ff6a4bce15e150a3R593 when removing the dock widget. (I know this isn't directly related, but I just remembered so thought I'd mention too.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adamltyson picture adamltyson  路  3Comments

tlambert03 picture tlambert03  路  3Comments

neuromusic picture neuromusic  路  3Comments

itamarst picture itamarst  路  3Comments

JoOkuma picture JoOkuma  路  3Comments