Skip to content

qt: Use better devicePixelRatio event to refresh scaling #30345

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
merged 1 commit into from
Jul 24, 2025
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())
Comment on lines +275 to +279
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect that both cases are a bit sloppy in that they connect/install on every showEvent. We typically have one, but there may be cases where the window lives longer and is hidden/shown multiple times. At least for connect() multiple calls result in multiple connections, i.e. firing multiple times.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

atleast for the event filter version

If filterObj has already been installed for this object, this function moves it so it acts as if it was installed last.

so it is OK to install it multiple times.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if I could find a better event just yet, but I'll try to take a look again later.


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