Reputation: 251
I'm attempting to develop a GUI that allows for tracking mouse coordinates in the PlotWidget, and displaying in a label elsewhere in the main window. I have attempted several times to emulate the crosshair example in the pyqtgraph documentation, but have not been able to get it to agree to do this. A part of the difficulty is that I am unable to understand how to access the mouse tracking I can enable in the QtDesigner.
I attempted to use the:
proxy = pg.SignalProxy(Plotted.scene().sigMouseMoved, rateLimit=60, slot=mouseMoved)
Plotted.scene().sigMouseMoved.connect(mouseMoved)
However, I do not quite understand what allows for this to "update" in real time, nor at what level I should have this statement. Should the
def mouseMoved(evt):
pos = evt[0]
if Plotted.sceneBoundingRect().contains(pos):
mousePoint = vb.mapSceneToView(pos)
index = int(mousePoint.x())
if index > 0 and index < len(x):
mousecoordinatesdisplay.setText("<span style='font-size: 12pt'>x=%0.1f, <span style='color: red'>y1=%0.1f</span>" % (mousePoint.x(), y[index], data2[index]))
vLine.setPos(mousePoint.x())
hLine.setPos(mousePoint.y())
part of the code be in the Ui_MainWindow class, or outside of it?
Upvotes: 0
Views: 4553
Reputation: 1
Modified the MouseMoved function to limit the precision of the displayed numbers for my application:
def MouseMoved(self, evt):
pos = evt
if self.graphWidget.sceneBoundingRect().contains(pos):
mousePoint = self.graphWidget.plotItem.vb.mapSceneToView(pos)
x = float("{0:.3f}".format(mousePoint.x()))
y = float("{0:.3f}".format(mousePoint.y()))
self.xylabel.setText(f"Cursor Position: {x, y}")
Upvotes: 0
Reputation: 251
I was able to get the updates to work by doing the following:
IN THE setupUi function:
Plotted = self.plot
vLine = pg.InfiniteLine(angle=90, movable=False)
hLine = pg.InfiniteLine(angle=0, movable=False)
Plotted.addItem(vLine, ignoreBounds=True)
Plotted.addItem(hLine, ignoreBounds=True)
Plotted.setMouseTracking(True)
Plotted.scene().sigMouseMoved.connect(self.mouseMoved)
def mouseMoved(self,evt):
pos = evt
if self.plot.sceneBoundingRect().contains(pos):
mousePoint = self.plot.plotItem.vb.mapSceneToView(pos)
self.mousecoordinatesdisplay.setText("<span style='font-size: 15pt'>X=%0.1f, <span style='color: black'>Y=%0.1f</span>" % (mousePoint.x(),mousePoint.y()))
self.plot.plotItem.vLine.setPos(mousePoint.x())
self.plot.plotItem.hLine.setPos(mousePoint.y()
Where the .mousecoordinatedisplay is a Label. It took me forever to figure out how to do this with use in the GUI from designer. It seems between pyqt4 and pyqt5 there was a change from the Qpointf, where the new Qpointf doesn't allow for indexing. By just passing the evt
variable, it is possible to map it without calling the evt[0]
.
Upvotes: 3