Skip to content

Qt: Fix HiDPI handling on X11/Windows #30399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/matplotlib/backends/backend_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
61 changes: 23 additions & 38 deletions lib/matplotlib/tests/test_backend_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading