0
votes

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?

2
I haven't used PyQtGraph in a while but you seem to be repeating your connection. SignalProxy should already be equivalent to ... .sigMouseMoved.connect(...). Also the mouse tracking should be happening in the pyqtgraph plot widget. I don't think any relation exists with other options you are activating on QtDesigner. The way this works is likely the way any SIGNAL/SLOT works. SignalProxy sends a signal every time the mouse is moved and runs whatever method you implemented for the connection. This isn't real time, it should only be launched when the mouse is moved. - armatita
Yes, this seems correct. However, I haven't been able to have an "updater" properly written in when, such that the connection to the signal/slot output (which I can confirm happens only when the mouse is moved, that's when I get an error message) updates a label in the code? - Jmegan042
So apparently you are accessing the method when you move the mouse. Its just that something happens after (likely inside the connected method). What is the error message? And exactly what part of the code is crashing? - armatita
Working off your suggestion as to how exactly it was passing the info (guess I wasn't thinking about it correctly), I found a work around where I was able to get it to work by leaving out the proxy statement, and getting rid of the pos = evt[0] - Jmegan042

2 Answers

2
votes

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].

0
votes

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}")