Source code for spacr.qt.widget_cleanup
"""Ownership-scoped cleanup for Qt objects libraries leave parentless.
Qt normally destroys a widget tree with its parent. pyqtgraph context menus
are an exception: ``PlotItem`` and ``ViewBox`` intentionally create them as
top-level windows and keep them through Python references. Closing a screen
therefore used to leave hundreds of live menu widgets behind, all of which a
later palette change had to visit.
The helper below walks only graphics scenes owned by the widget being closed.
It never sweeps ``QApplication`` and never invokes Python's cycle collector,
so it cannot collect an unrelated live QThread wrapper.
"""
from __future__ import annotations
import sys
from typing import Any