PyQt mousePressEvent - 获取被点击的对象? [英] PyQt mousePressEvent - get object that was clicked on?

查看:386
本文介绍了PyQt mousePressEvent - 获取被点击的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PyQt 和 PyQtGraph 来构建一个相对简单的绘图 UI.作为其中的一部分,我有一个图形视图(pyqtgraph 的 graphicslayoutwidget),其中包含由用户动态添加的 PlotItem.

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.

我想要实现的是允许用户通过双击它来选择一个 PlotItem.

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.

我的大部分搜索结果都试图为某些按钮重新实现 mousePressEvent.我已经阅读了一些关于事件过滤器的内容,但我不确定这是否是必要的解决方案.

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.

重复:

pyqtgraph:当我点击一个 PlotItem 我怎么知道哪个项目被点击了

推荐答案

一种策略是连接到GraphicsScene.sigMouseClicked,然后询问场景鼠标光标下有哪些项目.

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)

这篇关于PyQt mousePressEvent - 获取被点击的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆