PyQt - QGraphicsItem not recieving mouse events
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/hoverEnterEvent in a class derived from QGraphicsItem (or one of it's subclasses). My attempt looks like this:
Code:
def __init__(self,*args):
self.setAcceptDrops(True)
self.setAcceptHoverEvents(True)
self.setEnabled(True)
self.setActive(True)
def mouseMoveEvent(self,event):
print "Enter!"
def hoverEnterEvent(self,event):
print "Enter!" ...
...
def draw(self):
p = self.parent
...
for xpix in lons:
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.
Note that I am also setting up the GraphicsView to receive events as-well like this, and these mouse move events *are* being received OK:
Code:
def __init__(self):
self.setScene(self.scene)
self.setMouseTracking(True)
self.setInteractive(False)
...
def mouseMoveEvent(self,event):
...
Any advice on how to make this work?
Re: PyQt - QGraphicsItem not recieving mouse events
Ah, just reread your post and found your problem:
You are setting the view to not interactive! Leave it interactive and it should work!
Joh
Re: PyQt - QGraphicsItem not recieving mouse events
Quote:
Originally Posted by
JohannesMunk
Ah, just reread your post and found your problem:
You are setting the view to not interactive! Leave it interactive and it should work!
Joh
It is true that the view needs to be set to interactive, but my real problem was I needed to do this:
Code:
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.
Re: PyQt - QGraphicsItem not recieving mouse events
Do you call the inherited eventhandler? If you don't then its evident that nothing gets propagated.
Joh