Skip to content

Commit 8a8924c

Browse files
committed
Initial HiDPI support for qt5agg backend
1 parent 390dab3 commit 8a8924c

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

lib/matplotlib/backends/backend_qt5.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,13 @@ def leaveEvent(self, event):
249249
QtWidgets.QApplication.restoreOverrideCursor()
250250
FigureCanvasBase.leave_notify_event(self, guiEvent=event)
251251

252+
def mouseEventCoords(self, pos):
253+
x,y = self.mouseEventCoords(pos)
254+
print(x,y)
255+
return x,y
256+
252257
def mousePressEvent(self, event):
253-
x = event.pos().x()
254-
# flipy so y=0 is bottom of canvas
255-
y = self.figure.bbox.height - event.pos().y()
258+
x,y = self.mouseEventCoords(event.pos())
256259
button = self.buttond.get(event.button())
257260
if button is not None:
258261
FigureCanvasBase.button_press_event(self, x, y, button,
@@ -261,9 +264,7 @@ def mousePressEvent(self, event):
261264
print('button pressed:', event.button())
262265

263266
def mouseDoubleClickEvent(self, event):
264-
x = event.pos().x()
265-
# flipy so y=0 is bottom of canvas
266-
y = self.figure.bbox.height - event.pos().y()
267+
x,y = self.mouseEventCoords(event.pos())
267268
button = self.buttond.get(event.button())
268269
if button is not None:
269270
FigureCanvasBase.button_press_event(self, x, y,
@@ -273,16 +274,12 @@ def mouseDoubleClickEvent(self, event):
273274
print('button doubleclicked:', event.button())
274275

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

282281
def mouseReleaseEvent(self, event):
283-
x = event.x()
284-
# flipy so y=0 is bottom of canvas
285-
y = self.figure.bbox.height - event.y()
282+
x,y = self.mouseEventCoords(event)
286283
button = self.buttond.get(event.button())
287284
if button is not None:
288285
FigureCanvasBase.button_release_event(self, x, y, button,
@@ -291,9 +288,7 @@ def mouseReleaseEvent(self, event):
291288
print('button released')
292289

293290
def wheelEvent(self, event):
294-
x = event.x()
295-
# flipy so y=0 is bottom of canvas
296-
y = self.figure.bbox.height - event.y()
291+
x,y = self.mouseEventCoords(event)
297292
# from QWheelEvent::delta doc
298293
if event.pixelDelta().x() == 0 and event.pixelDelta().y() == 0:
299294
steps = event.angleDelta().y() / 120

lib/matplotlib/backends/backend_qt5agg.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def paintEvent(self, e):
100100
qImage = QtGui.QImage(stringBuffer, self.renderer.width,
101101
self.renderer.height,
102102
QtGui.QImage.Format_ARGB32)
103+
qImage.setDevicePixelRatio(self._dpi_ratio)
103104
# get the rectangle for the image
104105
rect = qImage.rect()
105106
p = QtGui.QPainter(self)
@@ -135,6 +136,7 @@ def paintEvent(self, e):
135136
stringBuffer = reg.to_string_argb()
136137
qImage = QtGui.QImage(stringBuffer, w, h,
137138
QtGui.QImage.Format_ARGB32)
139+
qImage.setDevicePixelRatio(self._dpi_ratio)
138140
# Adjust the stringBuffer reference count to work
139141
# around a memory leak bug in QImage() under PySide on
140142
# Python 3.x
@@ -222,6 +224,7 @@ def __init__(self, figure):
222224
super(FigureCanvasQTAgg, self).__init__(figure=figure)
223225
self._drawRect = None
224226
self.blitbox = []
227+
self._dpi_ratio = self.devicePixelRatio()
225228
self.setAttribute(QtCore.Qt.WA_OpaquePaintEvent)
226229

227230

0 commit comments

Comments
 (0)