Obtaining image coordinates at mouse pointer for an image item in GraphicsLayoutWidget

3,313 views
Skip to first unread message

Peter Spuhler

unread,
Aug 30, 2013, 11:45:07 AM8/30/13
to pyqt...@googlegroups.com
I have a GraphicsLayoutWidget embedded in a QT application. This Widget contains an image item and histogram as well as some ROI items. I would like to be able to obtain the x,y positions and image value under the mouse cursor as I hover over the image inside the widget. It's not immediately obvious to me how to do this. 2 things I've tried is subclassing graphicslayoutwidget and sigSceneMouseMoved. Sofar not successful with either one of these. I'm sure I'm missing something obvious. Any suggestions?

Luke Campagnola

unread,
Aug 30, 2013, 4:08:53 PM8/30/13
to pyqt...@googlegroups.com
On Fri, Aug 30, 2013 at 9:45 AM, Peter Spuhler <pe...@spuhler.net> wrote:
I have a GraphicsLayoutWidget embedded in a QT application. This Widget contains an image item and histogram as well as some ROI items. I would like to be able to obtain the x,y positions and image value under the mouse cursor as I hover over the image inside the widget. It's not immediately obvious to me how to do this. 2 things I've tried is subclassing graphicslayoutwidget and sigSceneMouseMoved. Sofar not successful with either one of these. I'm sure I'm missing something obvious. Any suggestions?

The easiest way is to connect some callback function to GraphicsScene.sigMouseMoved, then map the scene position to the image coordinate system. Here is an example:

import pyqtgraph as pg
import numpy as np
pg.mkQApp()
w = pg.GraphicsLayoutWidget()
w.show()
vb = w.addViewBox()
img = pg.ImageItem(np.random.normal(size=(100,100)))
vb.addItem(img)
def mouseMoved(pos):
    print "Image position:", img.mapFromScene(pos)

w.scene().sigMouseMoved.connect(mouseMoved)
 

Peter Spuhler

unread,
Aug 30, 2013, 7:59:45 PM8/30/13
to pyqt...@googlegroups.com
That works very nicely.  Thanks!
Reply all
Reply to author
Forward
0 new messages