pyqt graphitem鼠标进入事件
我正在尝试连接到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(部分)解决方案是这样的:
这确保各个项目处理自己的事件,似乎是在组捕获它们之前。
我仍然有一个问题,因为我的 GraphicsView 覆盖了 mouseMoveEvent,并且启用后,没有事件传播到场景项。
编辑:似乎第二个问题的解决方案是从视图 mouseMoveEvent 处理程序调用基类 mouseMoveEvent 处理程序,例如
The (partial) solution was this:
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.