diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 2bb642acf5a0..cf7889678553 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -1845,6 +1845,7 @@ def resize_event(self): s = 'resize_event' event = ResizeEvent(s, self) self.callbacks.process(s, event) + self.draw_idle() def close_event(self, guiEvent=None): """Pass a `CloseEvent` to all functions connected to ``close_event``. diff --git a/lib/matplotlib/backends/backend_qt5.py b/lib/matplotlib/backends/backend_qt5.py index 2e895195f906..eb2248f69fa0 100644 --- a/lib/matplotlib/backends/backend_qt5.py +++ b/lib/matplotlib/backends/backend_qt5.py @@ -359,9 +359,10 @@ def resizeEvent(self, event): winch = w / dpival hinch = h / dpival self.figure.set_size_inches(winch, hinch, forward=False) - FigureCanvasBase.resize_event(self) - self.draw_idle() + # pass back into Qt to let it finish QtWidgets.QWidget.resizeEvent(self, event) + # emit our resize events + FigureCanvasBase.resize_event(self) def sizeHint(self): w, h = self.get_width_height() diff --git a/lib/matplotlib/backends/backend_qt5agg.py b/lib/matplotlib/backends/backend_qt5agg.py index bb5bd64396c7..b4c7104f0ef2 100644 --- a/lib/matplotlib/backends/backend_qt5agg.py +++ b/lib/matplotlib/backends/backend_qt5agg.py @@ -56,7 +56,10 @@ def paintEvent(self, e): In Qt, all drawing should be done inside of here when a widget is shown onscreen. """ - + # if there is a pending draw, run it now as we need the updated render + # to paint the widget + if self._agg_draw_pending: + self.__draw_idle_agg() # As described in __init__ above, we need to be careful in cases with # mixed resolution displays if dpi_ratio is changing between painting # events. @@ -72,7 +75,6 @@ def paintEvent(self, e): # since the latter doesn't guarantee that the event will be emitted # straight away, and this causes visual delays in the changes. self.resizeEvent(event) - QtWidgets.QApplication.instance().processEvents() # resizeEvent triggers a paintEvent itself, so we exit this one. return @@ -138,6 +140,8 @@ def draw_idle(self): QtCore.QTimer.singleShot(0, self.__draw_idle_agg) def __draw_idle_agg(self, *args): + if not self._agg_draw_pending: + return if self.height() < 0 or self.width() < 0: self._agg_draw_pending = False return diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index d47fa4d2d935..8d4273cfe1b8 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -708,14 +708,14 @@ def set_size_inches(self, w, h=None, forward=True): Usage :: - fig.set_size_inches(w,h) # OR - fig.set_size_inches((w,h)) + fig.set_size_inches(w, h) # OR + fig.set_size_inches((w, h)) optional kwarg *forward=True* will cause the canvas size to be automatically updated; e.g., you can resize the figure window from the shell - ACCEPTS: a w,h tuple with w,h in inches + ACCEPTS: a w, h tuple with w, h in inches See Also -------- diff --git a/lib/matplotlib/tests/test_backend_qt5.py b/lib/matplotlib/tests/test_backend_qt5.py index 3f4bab6fa4f1..8e903653fc25 100644 --- a/lib/matplotlib/tests/test_backend_qt5.py +++ b/lib/matplotlib/tests/test_backend_qt5.py @@ -145,6 +145,11 @@ def test_dpi_ratio_change(): qt_canvas.draw() qApp.processEvents() + # this second processEvents is required to fully run the draw. + # On `update` we notice the DPI has changed and trigger a + # resize event to refresh, the second processEvents is + # required to process that and fully update the window sizes. + qApp.processEvents() # The DPI and the renderer width/height change assert fig.dpi == 240