pyqt graphitem鼠标进入事件

发布于 2024-10-27 20:15:47 字数 1198 浏览 2 评论 0原文

我正在尝试连接到 QGraphicsItems 的鼠标输入事件,该事件被放置在 QGraphicsScene 上并通过 QGraphicsView 进行可视化。据我了解,为此重写的方法是从 QGraphicsItem (或其子类之一)派生的类中的 DragEnterEvent 。我的尝试如下所示:

class StaPoly(QtGui.QGraphicsPolygonItem):

    def __init__(self,*args):
        QtGui.QGraphicsPolygonItem.__init__(self,*args)
        self.setAcceptDrops(True)

    def dragEnterEvent(self,event):
        print "Enter!"        

...

    def draw(self):        
        p       = self.parent

        self.group = QtGui.QGraphicsItemGroup(scene=p.scene)

...

        for xpix in lons: 
            poly = QtGui.QPolygonF()
            poly << QtCore.QPointF(xpix-symw,ypix)
            poly << QtCore.QPointF(xpix,ypix+symh)                
            poly << QtCore.QPointF(xpix+symw,ypix)                
            poly << QtCore.QPointF(xpix,ypix-symh)                                                

            item = StaPoly(poly)
            item.setPen(QtGui.QColor(color))
            item.setBrush(QtGui.QColor(color))  
            self.group.addToGroup(item)   

我希望上面的片段能够清楚地说明我想要做什么。请注意,显示完全按照我的意愿生成,没有问题 - 但是绘制的多边形没有响应输入事件 - 我没有看到任何证据表明 DragEnterEvent() 正在被调用。

I am trying to connect to the mouse-enter event of QGraphicsItems that are placed onto a QGraphicsScene and visualized through a QGraphicsView. From what I understand, the method to override for this is dragEnterEvent in a class derived from QGraphicsItem (or one of it's subclasses). My attempt looks like this:

class StaPoly(QtGui.QGraphicsPolygonItem):

    def __init__(self,*args):
        QtGui.QGraphicsPolygonItem.__init__(self,*args)
        self.setAcceptDrops(True)

    def dragEnterEvent(self,event):
        print "Enter!"        

...

    def draw(self):        
        p       = self.parent

        self.group = QtGui.QGraphicsItemGroup(scene=p.scene)

...

        for xpix in lons: 
            poly = QtGui.QPolygonF()
            poly << QtCore.QPointF(xpix-symw,ypix)
            poly << QtCore.QPointF(xpix,ypix+symh)                
            poly << QtCore.QPointF(xpix+symw,ypix)                
            poly << QtCore.QPointF(xpix,ypix-symh)                                                

            item = StaPoly(poly)
            item.setPen(QtGui.QColor(color))
            item.setBrush(QtGui.QColor(color))  
            self.group.addToGroup(item)   

I hope the above snippets make it clear what I am trying to do. Note that the display is generated exactly as I desire, no issue there - however the polygons that get drawn are not responding to the enter-event - I am not seeing any evidence that dragEnterEvent() is being called.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℉服软 2024-11-03 20:15:47

(部分)解决方案是这样的:

self.group.setHandlesChildEvents(False)     

这确保各个项目处理自己的事件,似乎是在组捕获它们之前。

我仍然有一个问题,因为我的 GraphicsView 覆盖了 mouseMoveEvent,并且启用后,没有事件传播到场景项。

编辑:似乎第二个问题的解决方案是从视图 mouseMoveEvent 处理程序调用基类 mouseMoveEvent 处理程序,例如

def mouseMoveEvent(self,event):
    QtGui.QGraphicsView.mouseMoveEvent(self, event)

The (partial) solution was this:

self.group.setHandlesChildEvents(False)     

This ensures that individual items handle their own events, it seems that before the group was capturing them.

I still have a problem in that my GraphicsView overrides the mouseMoveEvent, and when enabled, no events get propagated to scene items.

EDIT: And it seems the solution to this second problem was to call the base class mouseMoveEvent handler from the Views mouseMoveEvent handler e.g.

def mouseMoveEvent(self,event):
    QtGui.QGraphicsView.mouseMoveEvent(self, event)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文