mouseMoved event on win.addViewBox

64 views
Skip to first unread message

ncu...@seas.upenn.edu

unread,
Feb 25, 2017, 5:46:12 PM2/25/17
to pyqtgraph
I am trying to recreate the mouse movement slot from the "Crosshair / Mouse Interaction" example using addViewBox instead of addPlot, but it doesn't seem to be mapping the position -> line correctly.  Has anyone tried this?

Here is the code

```
import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtGui, QtCore
from pyqtgraph.Point import Point


app = QtGui.QApplication([])
win = pg.GraphicsWindow()

win.setWindowTitle('pyqtgraph example: crosshair')
label = pg.LabelItem('NICK', justify='center')
win.addItem(label)

p1 = win.addViewBox(row=1, col=0)
image = pg.ImageItem(np.random.randn(50,50))
p1.addItem(image)

vLine = pg.InfiniteLine(angle=90, movable=False)
hLine = pg.InfiniteLine(angle=0, movable=False)
p1.addItem(vLine, ignoreBounds=True)
p1.addItem(hLine, ignoreBounds=True)

def mouseMoved(evt):
pos = evt[0]
if p1.sceneBoundingRect().contains(pos):
mousePoint = p1.mapSceneToView(pos)
vLine.setPos(pos.x())
hLine.setPos(pos.y())
proxy = pg.SignalProxy(p1.scene().sigMouseMoved, rateLimit=60, slot=mouseMoved)
```

ncu...@seas.upenn.edu

unread,
Feb 25, 2017, 6:14:19 PM2/25/17
to pyqtgraph
Never mind.. Clearly there is a bug above in my code using 'pos' instead of 'mousePoint' but regardless I got it to work with the following for others to reference:



import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtGui, QtCore
from pyqtgraph.Point import Point


#app = QtGui.QApplication([])
pg.mkQApp()

win = pg.GraphicsLayoutWidget()
win.show()
win.setWindowTitle('pyqtgraph example: crosshair')

p1 = win.addViewBox(row=1, col=0)
image = pg.ImageItem()
p1.addItem(image)
image.setImage(np.random.randn(100,100))

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
def mouseMoved(evt):
pos = evt[0]
if p1.sceneBoundingRect().contains(pos):
mousePoint = p1.mapSceneToView(pos)
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_()
Reply all
Reply to author
Forward
0 new messages