Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1568a43

Browse files
committed
PoC: GUI-native crosshair cursor
Addresses #30515
1 parent 0394f4e commit 1568a43

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/matplotlib/backends/backend_qt.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ def __init__(self, figure=None):
239239

240240
self.setAttribute(QtCore.Qt.WidgetAttribute.WA_OpaquePaintEvent)
241241
self.setMouseTracking(True)
242+
self.mouse_xy = (0, 0)
242243
self.resize(*self.get_width_height())
243244

244245
palette = QtGui.QPalette(QtGui.QColor("white"))
@@ -344,8 +345,10 @@ def mouseDoubleClickEvent(self, event):
344345
def mouseMoveEvent(self, event):
345346
if self.figure is None:
346347
return
348+
self.mouse_xy = self.mouseEventCoords(event)
349+
self.repaint()
347350
MouseEvent("motion_notify_event", self,
348-
*self.mouseEventCoords(event),
351+
*self.mouse_xy,
349352
buttons=self._mpl_buttons(event.buttons()),
350353
modifiers=self._mpl_modifiers(),
351354
guiEvent=event)._process()

lib/matplotlib/backends/backend_qtagg.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ def paintEvent(self, event):
6868
ctypes.c_long.from_address(id(buf)).value = 1
6969

7070
self._draw_rect_callback(painter)
71+
72+
figx = self.mouse_xy[0] / rect.width()
73+
figy = 1 - self.mouse_xy[1] / rect.height()
74+
x0 = rect.left()
75+
x1 = rect.left() + rect.width()
76+
y0 = rect.top()
77+
y1 = rect.top() + rect.height()
78+
x = rect.left() + int(figx * rect.width())
79+
y = rect.top() + int(figy * rect.height())
80+
painter.setPen(QtGui.QPen(QtCore.Qt.GlobalColor.red))
81+
painter.drawLine(x0, y, x1, y)
82+
painter.drawLine(x, y0, x, y1)
83+
7184
finally:
7285
painter.end()
7386

0 commit comments

Comments
 (0)