Skip to content

Commit 52a204b

Browse files
committed
Deprecate NavigationToolbar2QT.parent.
This attribute shadows the normal Qt method parent() (which also returns the parent window...), which is a source of confusion. After the deprecation period passes, the parent property can be deleted and parent() will simply be the method inherited from QToolBar. Note that the attribute is not present on other backend toolbars either.
1 parent 757fb24 commit 52a204b

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

doc/api/next_api_changes/deprecations.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,3 +370,9 @@ mathtext ``Glue`` helper classes
370370
The ``Fil``, ``Fill``, ``Filll``, ``NegFil``, ``NegFill``, ``NegFilll``, and
371371
``SsGlue`` classes in the :mod:`matplotlib.mathtext` module are deprecated.
372372
As an alternative, directly construct glue instances with ``Glue("fil")``, etc.
373+
374+
NavigationToolbar2QT.parent
375+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
376+
This attribute is deprecated. In order to access the parent window, use
377+
``toolbar.canvas.parent()``. Once the deprecation period is elapsed, it will
378+
also be accessible as ``toolbar.parent()``.

lib/matplotlib/backends/backend_qt5.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -660,14 +660,19 @@ class NavigationToolbar2QT(NavigationToolbar2, QtWidgets.QToolBar):
660660
def __init__(self, canvas, parent, coordinates=True):
661661
"""coordinates: should we show the coordinates on the right?"""
662662
self.canvas = canvas
663-
self.parent = parent
663+
self._parent = parent
664664
self.coordinates = coordinates
665665
self._actions = {}
666666
"""A mapping of toolitem method names to their QActions"""
667667

668668
QtWidgets.QToolBar.__init__(self, parent)
669669
NavigationToolbar2.__init__(self, canvas)
670670

671+
@cbook.deprecated("3.3", alternative="self.canvas.parent()")
672+
@property
673+
def parent(self):
674+
return self._parent
675+
671676
def _icon(self, name, color=None):
672677
if is_pyqt5():
673678
name = name.replace('.png', '_large.png')
@@ -718,7 +723,7 @@ def edit_parameters(self):
718723
axes = self.canvas.figure.get_axes()
719724
if not axes:
720725
QtWidgets.QMessageBox.warning(
721-
self.parent, "Error", "There are no axes to edit.")
726+
self.canvas.parent(), "Error", "There are no axes to edit.")
722727
return
723728
elif len(axes) == 1:
724729
ax, = axes
@@ -735,7 +740,8 @@ def edit_parameters(self):
735740
if titles[i] in duplicate_titles:
736741
titles[i] += f" (id: {id(ax):#x})" # Deduplicate titles.
737742
item, ok = QtWidgets.QInputDialog.getItem(
738-
self.parent, 'Customize', 'Select axes:', titles, 0, False)
743+
self.canvas.parent(),
744+
'Customize', 'Select axes:', titles, 0, False)
739745
if not ok:
740746
return
741747
ax = axes[titles.index(item)]

0 commit comments

Comments
 (0)