Skip to content

Initial HiDPI support for qt5agg backend #6389

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

Closed
wants to merge 1 commit into from
Closed
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
31 changes: 14 additions & 17 deletions lib/matplotlib/backends/backend_qt5.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,14 @@ def leaveEvent(self, event):
QtWidgets.QApplication.restoreOverrideCursor()
FigureCanvasBase.leave_notify_event(self, guiEvent=event)

def mouseEventCoords(self, pos):
x = pos.x() * self.devicePixelRatio()
# flip y so y=0 is bottom of canvas
y = self.figure.bbox.height - pos.y() * self.devicePixelRatio()
return x, y

def mousePressEvent(self, event):
x = event.pos().x()
# flipy so y=0 is bottom of canvas
y = self.figure.bbox.height - event.pos().y()
x, y = self.mouseEventCoords(event.pos())
button = self.buttond.get(event.button())
if button is not None:
FigureCanvasBase.button_press_event(self, x, y, button,
Expand All @@ -261,9 +265,7 @@ def mousePressEvent(self, event):
print('button pressed:', event.button())

def mouseDoubleClickEvent(self, event):
x = event.pos().x()
# flipy so y=0 is bottom of canvas
y = self.figure.bbox.height - event.pos().y()
x, y = self.mouseEventCoords(event.pos())
button = self.buttond.get(event.button())
if button is not None:
FigureCanvasBase.button_press_event(self, x, y,
Expand All @@ -273,16 +275,12 @@ def mouseDoubleClickEvent(self, event):
print('button doubleclicked:', event.button())

def mouseMoveEvent(self, event):
x = event.x()
# flipy so y=0 is bottom of canvas
y = self.figure.bbox.height - event.y()
x, y = self.mouseEventCoords(event)
FigureCanvasBase.motion_notify_event(self, x, y, guiEvent=event)
# if DEBUG: print('mouse move')

def mouseReleaseEvent(self, event):
x = event.x()
# flipy so y=0 is bottom of canvas
y = self.figure.bbox.height - event.y()
x, y = self.mouseEventCoords(event)
button = self.buttond.get(event.button())
if button is not None:
FigureCanvasBase.button_release_event(self, x, y, button,
Expand All @@ -291,9 +289,7 @@ def mouseReleaseEvent(self, event):
print('button released')

def wheelEvent(self, event):
x = event.x()
# flipy so y=0 is bottom of canvas
y = self.figure.bbox.height - event.y()
x, y = self.mouseEventCoords(event)
# from QWheelEvent::delta doc
if event.pixelDelta().x() == 0 and event.pixelDelta().y() == 0:
steps = event.angleDelta().y() / 120
Expand Down Expand Up @@ -323,8 +319,9 @@ def keyReleaseEvent(self, event):
print('key release', key)

def resizeEvent(self, event):
w = event.size().width()
h = event.size().height()
dpi_ratio = getattr(self, '_dpi_ratio', 1)
w = event.size().width() * dpi_ratio
h = event.size().height() * dpi_ratio
if DEBUG:
print('resize (%d x %d)' % (w, h))
print("FigureCanvasQt.resizeEvent(%d, %d)" % (w, h))
Expand Down
3 changes: 3 additions & 0 deletions lib/matplotlib/backends/backend_qt5agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def paintEvent(self, e):
qImage = QtGui.QImage(stringBuffer, self.renderer.width,
self.renderer.height,
QtGui.QImage.Format_ARGB32)
qImage.setDevicePixelRatio(self._dpi_ratio)
# get the rectangle for the image
rect = qImage.rect()
p = QtGui.QPainter(self)
Expand Down Expand Up @@ -135,6 +136,7 @@ def paintEvent(self, e):
stringBuffer = reg.to_string_argb()
qImage = QtGui.QImage(stringBuffer, w, h,
QtGui.QImage.Format_ARGB32)
qImage.setDevicePixelRatio(self._dpi_ratio)
# Adjust the stringBuffer reference count to work
# around a memory leak bug in QImage() under PySide on
# Python 3.x
Expand Down Expand Up @@ -222,6 +224,7 @@ def __init__(self, figure):
super(FigureCanvasQTAgg, self).__init__(figure=figure)
self._drawRect = None
self.blitbox = []
self._dpi_ratio = self.devicePixelRatio()
self.setAttribute(QtCore.Qt.WA_OpaquePaintEvent)


Expand Down