PyQt mousePressEvent - get object that was clicked on?

核能气质少年 提交于 2019-12-11 16:17:00

问题


I'm using PyQt and PyQtGraph to build a relatively simple plotting UI. As part of this I have a graphicsview (pyqtgraph's graphicslayoutwidget) that has PlotItems dynamically added to it by the user.

What I'm trying to achieve is allowing the user to select a PlotItem by double clicking on it.

It's simple enough to get if the user has double clicked somewhere within the widget window, but I can't seem to figure out how to return what was clicked on.

Most of my search results have come up with trying to reimplement mousePressEvent for certain pushbuttons. I've read a bit about event filters, but I'm not sure if that's the necessary solution here.

I'm not sure what other information might be useful for helping answer this question, so if it's unclear what I'm asking let me know so I can clarify.

Edit:

Duplicate of this:

pyqtgraph: When I click on a PlotItem how do I know which item has been clicked


回答1:


One strategy is to connect to GraphicsScene.sigMouseClicked and then ask the scene which items are under the mouse cursor.

This should get you part way there:

import pyqtgraph as pg

w = pg.GraphicsWindow()
for i in range(4):
    w.addPlot(0, i)

def onClick(event):
    items = w.scene().items(event.scenePos())
    print "Plots:", [x for x in items if isinstance(x, pg.PlotItem)]

w.scene().sigMouseClicked.connect(onClick)


来源:https://stackoverflow.com/questions/27222016/pyqt-mousepressevent-get-object-that-was-clicked-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!