Skip to content

Backport PR #30345 on branch v3.10.x (qt: Use better devicePixelRatio event to refresh scaling) #30353

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

Merged
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
13 changes: 11 additions & 2 deletions lib/matplotlib/backends/backend_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,21 @@ def _update_screen(self, screen):
screen.physicalDotsPerInchChanged.connect(self._update_pixel_ratio)
screen.logicalDotsPerInchChanged.connect(self._update_pixel_ratio)

def eventFilter(self, source, event):
if event.type() == QtCore.QEvent.Type.DevicePixelRatioChange:
self._update_pixel_ratio()
return super().eventFilter(source, event)

def showEvent(self, event):
# Set up correct pixel ratio, and connect to any signal changes for it,
# once the window is shown (and thus has these attributes).
window = self.window().windowHandle()
window.screenChanged.connect(self._update_screen)
self._update_screen(window.screen())
current_version = tuple(int(x) for x in QtCore.qVersion().split('.', 2)[:2])
if current_version >= (6, 6):
window.installEventFilter(self)
else:
window.screenChanged.connect(self._update_screen)
self._update_screen(window.screen())

def set_cursor(self, cursor):
# docstring inherited
Expand Down
21 changes: 14 additions & 7 deletions lib/matplotlib/tests/test_backend_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def on_key_press(event):


@pytest.mark.backend('QtAgg', skip_on_importerror=True)
def test_device_pixel_ratio_change():
def test_device_pixel_ratio_change(qt_core):
"""
Make sure that if the pixel ratio changes, the figure dpi changes but the
widget remains the same logical size.
Expand All @@ -154,11 +154,19 @@ def test_device_pixel_ratio_change():
def set_device_pixel_ratio(ratio):
p.return_value = ratio

# The value here doesn't matter, as we can't mock the C++ QScreen
# object, but can override the functional wrapper around it.
# Emitting this event is simply to trigger the DPI change handler
# in Matplotlib in the same manner that it would occur normally.
screen.logicalDotsPerInchChanged.emit(96)
window = qt_canvas.window().windowHandle()
current_version = tuple(
int(x) for x in qt_core.qVersion().split('.', 2)[:2])
if current_version >= (6, 6):
qt_core.QCoreApplication.sendEvent(
window,
qt_core.QEvent(qt_core.QEvent.Type.DevicePixelRatioChange))
else:
# The value here doesn't matter, as we can't mock the C++ QScreen
# object, but can override the functional wrapper around it.
# Emitting this event is simply to trigger the DPI change handler
# in Matplotlib in the same manner that it would occur normally.
window.screen().logicalDotsPerInchChanged.emit(96)

qt_canvas.draw()
qt_canvas.flush_events()
Expand All @@ -168,7 +176,6 @@ def set_device_pixel_ratio(ratio):

qt_canvas.manager.show()
size = qt_canvas.size()
screen = qt_canvas.window().windowHandle().screen()
set_device_pixel_ratio(3)

# The DPI and the renderer width/height change
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy