diff --git a/lib/matplotlib/backends/backend_qt.py b/lib/matplotlib/backends/backend_qt.py index 401ce0b0b754..dd614e516de5 100644 --- a/lib/matplotlib/backends/backend_qt.py +++ b/lib/matplotlib/backends/backend_qt.py @@ -273,6 +273,7 @@ def showEvent(self, event): window = self.window().windowHandle() current_version = tuple(int(x) for x in QtCore.qVersion().split('.', 2)[:2]) if current_version >= (6, 6): + self._update_pixel_ratio() window.installEventFilter(self) else: window.screenChanged.connect(self._update_screen) diff --git a/lib/matplotlib/tests/test_backend_qt.py b/lib/matplotlib/tests/test_backend_qt.py index 6e147fd14380..db33986e9542 100644 --- a/lib/matplotlib/tests/test_backend_qt.py +++ b/lib/matplotlib/tests/test_backend_qt.py @@ -175,45 +175,30 @@ def set_device_pixel_ratio(ratio): assert qt_canvas.device_pixel_ratio == ratio qt_canvas.manager.show() + qt_canvas.draw() + qt_canvas.flush_events() size = qt_canvas.size() - set_device_pixel_ratio(3) - - # The DPI and the renderer width/height change - assert fig.dpi == 360 - assert qt_canvas.renderer.width == 1800 - assert qt_canvas.renderer.height == 720 - - # The actual widget size and figure logical size don't change. - assert size.width() == 600 - assert size.height() == 240 - assert qt_canvas.get_width_height() == (600, 240) - assert (fig.get_size_inches() == (5, 2)).all() - - set_device_pixel_ratio(2) - - # The DPI and the renderer width/height change - assert fig.dpi == 240 - assert qt_canvas.renderer.width == 1200 - assert qt_canvas.renderer.height == 480 - - # The actual widget size and figure logical size don't change. - assert size.width() == 600 - assert size.height() == 240 - assert qt_canvas.get_width_height() == (600, 240) - assert (fig.get_size_inches() == (5, 2)).all() - - set_device_pixel_ratio(1.5) - - # The DPI and the renderer width/height change - assert fig.dpi == 180 - assert qt_canvas.renderer.width == 900 - assert qt_canvas.renderer.height == 360 - - # The actual widget size and figure logical size don't change. - assert size.width() == 600 - assert size.height() == 240 - assert qt_canvas.get_width_height() == (600, 240) - assert (fig.get_size_inches() == (5, 2)).all() + + options = [ + (None, 360, 1800, 720), # Use ratio at startup time. + (3, 360, 1800, 720), # Change to same ratio. + (2, 240, 1200, 480), # Change to different ratio. + (1.5, 180, 900, 360), # Fractional ratio. + ] + for ratio, dpi, width, height in options: + if ratio is not None: + set_device_pixel_ratio(ratio) + + # The DPI and the renderer width/height change + assert fig.dpi == dpi + assert qt_canvas.renderer.width == width + assert qt_canvas.renderer.height == height + + # The actual widget size and figure logical size don't change. + assert size.width() == 600 + assert size.height() == 240 + assert qt_canvas.get_width_height() == (600, 240) + assert (fig.get_size_inches() == (5, 2)).all() @pytest.mark.backend('QtAgg', skip_on_importerror=True)