How to determine which widget the mouse pointer is on?

33 views
Skip to first unread message

song whale

unread,
May 13, 2022, 11:29:40 PM5/13/22
to pyqtgraph
Hello. If there are multiple docks and their corresponding widgets inside one DockArea, how can I get the mouse pointer coordinates?


def mouseMoved(pos): 
     qwidget.pwidget  .getViewBox().mapSceneToView(pos)   
     xCor = mousePoint.x() 
     yCor = mousePoint.y()   
     print( xCor, yCor)   

qwidget.pwidget.scene().sigMouseMoved.connect(mouseMoved)


If I use the fuction above, I need to specify which widget the mouse is on in advance. 
Are there any convenient ways to determine the mouse position in a situation where there are multiple widgets, or is there a method that tells you which widget the mouse is on?

Patrick

unread,
May 15, 2022, 8:13:59 PM5/15/22
to pyqtgraph
Hi,

There's probably a few ways to do this, but the simplest modification to your code would be to use a lambda expression to wrap the mouseMoved function and pass in the appropriate widget. It might look something like this:

def mouseMoved(widget, pos): 
     widget.getViewBox().mapSceneToView(pos)   
     xCor = mousePoint.x() 
     yCor = mousePoint.y()   
     print( xCor, yCor)   

for w in [pwidget1, pwidget2, pwidget3]:
    w.scene().sigMouseMoved.connect(lambda pos: mouseMoved(w, pos))

I'll also add that if you ever need to do some complicated event handling that is not possible in pyqtgraph, then remember that everything in pyqtgraph is a Qt object, which means you can go directly to the Qt event layer to catch and manipulate events at a low level. The EventFilter is very powerful in this regard: https://doc.qt.io/qtforpython/overviews/eventsandfilters.html

Patrick
Reply all
Reply to author
Forward
0 new messages