Results 1 to 4 of 4

Thread: PyQt - QGraphicsItem not recieving mouse events

  1. #1
    Join Date
    Apr 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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:
    Qt Code:
    1. class StaPoly(QtGui.QGraphicsPolygonItem):
    2.  
    3. def __init__(self,*args):
    4. QtGui.QGraphicsPolygonItem.__init__(self,*args)
    5. self.setAcceptDrops(True)
    6. self.setAcceptHoverEvents(True)
    7. self.setEnabled(True)
    8. self.setActive(True)
    9.  
    10. def mouseMoveEvent(self,event):
    11. print "Enter!"
    12.  
    13. def hoverEnterEvent(self,event):
    14. print "Enter!" ...
    15.  
    16. ...
    17.  
    18. def draw(self):
    19. p = self.parent
    20.  
    21. self.group = QtGui.QGraphicsItemGroup(scene=p.scene)
    22.  
    23. ...
    24. for xpix in lons:
    25. poly = QtGui.QPolygonF()
    26. poly << QtCore.QPointF(xpix-symw,ypix)
    27. poly << QtCore.QPointF(xpix,ypix+symh)
    28. poly << QtCore.QPointF(xpix+symw,ypix)
    29. poly << QtCore.QPointF(xpix,ypix-symh)
    30.  
    31. item = StaPoly(poly)
    32. item.setPen(QtGui.QColor(color))
    33. item.setBrush(QtGui.QColor(color))
    34. self.group.addToGroup(item)
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. class MyView(QtGui.QGraphicsView):
    2.  
    3. def __init__(self):
    4. QtGui.QGraphicsView.__init__(self)
    5. self.scene = QtGui.QGraphicsScene()
    6. self.setScene(self.scene)
    7. self.setMouseTracking(True)
    8. self.setInteractive(False)
    9.  
    10. ...
    11.  
    12. def mouseMoveEvent(self,event):
    13.  
    14. ...
    To copy to clipboard, switch view to plain text mode 

    Any advice on how to make this work?

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  3. #3
    Join Date
    Apr 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: PyQt - QGraphicsItem not recieving mouse events

    Quote Originally Posted by JohannesMunk View Post
    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:

    Qt Code:
    1. self.group.setHandlesChildEvents(False)
    To copy to clipboard, switch view to plain text mode 

    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.

  4. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

Similar Threads

  1. Replies: 5
    Last Post: 27th April 2010, 12:04
  2. QGraphicsItem no mouse events called
    By munna in forum Qt Programming
    Replies: 11
    Last Post: 9th December 2009, 15:43
  3. mouse events handling with QGraphicsItem
    By trallallero in forum Qt Programming
    Replies: 3
    Last Post: 21st October 2009, 15:15
  4. QGraphicsItem reimplemented mouse events
    By aarelovich in forum Qt Programming
    Replies: 12
    Last Post: 24th July 2009, 13:56
  5. QGraphicsItem mouse events
    By zgulser in forum Qt Programming
    Replies: 13
    Last Post: 11th February 2009, 12:19

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.