简体   繁体   English

如何检测在pyside中绘制的椭圆上的鼠标单击?

[英]How to detect mouse click on ellipse drawn in pyside?

I am using QPainter within a QWidget to draw a bunch of ellipses on a black background as follows: 我正在QWidget使用QPainter在黑色背景上绘制一堆椭圆,如下所示:

paint = QPainter()
paint.begin(self)

paint.setBrush(Qt.black)
paint.drawRect(event.rect())

brush = ...
paint.setBrush(brush)
paint.drawEllipse(center, rad, rad)

After a bunch of ellipses were drawn, and then I want to detect a mouse click on one of such an existing ellipse. 绘制了一堆椭圆后,然后我要检测鼠标在这样一个现有椭圆之一上的单击。 I did not find any obvious in the documentation for QPainter . 我在QPainter的文档中没有发现任何明显的内容。

In case there is something else to be used instead of QPainter , please provide an example that shows my above example in the other framework. 如果要使用其他代替QPainter ,请提供一个示例,在另一个框架中显示我上面的示例。

You will need to detect the custom area yourself as follows: 您将需要自己检测自定义区域,如下所示:

def mousePressEvent(self, event):
    ''' You will have to implement the contain algorithm yourself'''
    if sel.fo(even.pos()):
        self.myMethod()

QGraphicsEllipseItem.contains() QGraphicsEllipseItem.contains()

Alternatively, you could look into the QGraphicsEllipseItem because it has the contains-logic implemented and offered . 另外,您可以查看QGraphicsEllipseItem因为它已实现并提供contains-logic

def mousePressEvent(self, event):
    if self.contains(event.pos()):
        self.myMethod()

and you create your object with the corresponding parameters: 并使用相应的参数创建对象:

scene = QGraphicsScene()
ellipseItem = MyGraphicsEllipseItem(centerx, centery, rad, rad)
scene.addItem(ellipseItem)

view = QGraphicsView(scene)
view.show()

scene.setBackgroundBrush(Qt.black)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM