I am trying to make a cross hair on my pyqtgraph interactive plots, which 
are embedded in a PyQt5 GUI thanks to the designer-qt5. I found a working 
code in the pyqtgraph "examples". A simplified WORKING example is posted 
below. Now I want the same, but the problem seems to be that I promoted a 
QGraphicsView() to a pg.PlotWidget in the designer, instead of 
pg.GraphicsWindow()? The Code does not work for me because my p1 is 
"pyqtgraph.widgets.PlotWidget.PlotWidget object" while in the example p1 is
"pyqtgraph.graphicsItems.PlotItem.PlotItem.PlotItem object".


So what should I do to make this example work for me?



> import numpy as np
> import pyqtgraph as pg
> from pyqtgraph.Qt import QtGui, QtCore
> from pyqtgraph.Point import Point
>
> pg.setConfigOption('background', '#ffffff')
> pg.setConfigOption('foreground', 'k')
> pg.setConfigOptions(antialias=True)  
>
> app = QtGui.QApplication([])
> win = pg.GraphicsWindow()
> win.setWindowTitle('pyqtgraph example: crosshair')
> label = pg.LabelItem(justify='right')
> win.addItem(label)
> p1 = win.addPlot(row=1, col=0)       
>
> p1.setAutoVisible(y=True)
>
> #create numpy arrays
> #make the numbers large to show that the xrange shows data from 10000 to all 
> the way 0
> data1 = 10000 + 15000 * pg.gaussianFilter(np.random.random(size=10000), 10) + 
> 3000 * np.random.random(size=10000)
>
> p1.plot(data1, pen="r")
>
> #cross hair
> vLine = pg.InfiniteLine(angle=90, movable=False)
> hLine = pg.InfiniteLine(angle=0, movable=False)
> p1.addItem(vLine, ignoreBounds=True)
> p1.addItem(hLine, ignoreBounds=True)
>
> vb = p1.vb 
>
> print(p1)
> print(vb)
>
> def mouseMoved(evt):
>     pos = evt[0]  ## using signal proxy turns original arguments into a tuple
>     if p1.sceneBoundingRect().contains(pos):
>         mousePoint = vb.mapSceneToView(pos)
>         index = int(mousePoint.x())
>         if index > 0 and index < len(data1):
>             label.setText("<span style='font-size: 12pt'>x=%0.1f,   <span 
> style='color: green'>y2=%0.1f</span>" % (mousePoint.x(), data1[index]))
>         vLine.setPos(mousePoint.x())
>         hLine.setPos(mousePoint.y())
>
> proxy = pg.SignalProxy(p1.scene().sigMouseMoved, rateLimit=60, 
> slot=mouseMoved)
> #p1.scene().sigMouseMoved.connect(mouseMoved)
>
> ## Start Qt event loop unless running in interactive mode or using pyside.
> if __name__ == '__main__':
>     import sys
>     if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
>         QtGui.QApplication.instance().exec_()
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pyqtgraph/8fd8114b-008a-4347-aba8-4e61064c0b9f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to