I create several subclassed QGraphicRectItems with parent/child relationships to keep them in lock step on the screen. For whatever reason, the hover events on the children are triggering the parent hover event, but I want all of the instances' hover events to be independent. Is there any way to keep this from happening? Does an independent event filter need to be used? Strangely, adding the parent item to the scene after the children makes the hover events independent, but then the motion is wrong if the parent is moved.
Here is a MWE:
import sys
from PyQt5 import QtCore, QtWidgets, QtGui
# Define a hoverable rectangle item to be used by the DefinitionGraphic
class HoverRectItem(QtWidgets.QGraphicsRectItem):
# Initialize
def __init__(self, x, y, width, height,
parent = None,
fill = QtCore.Qt.blue,
highlight = QtCore.Qt.red,
name = ''):
super().__init__(x, y, width, height, parent = parent)
# Set fill and highlight colors
self.fill_brush = QtGui.QBrush(QtGui.QColor(fill))
self.hover_brush = QtGui.QBrush(QtGui.QColor(highlight))
self.setBrush(self.fill_brush)
self.name = name
# Enable hover events
self.setAcceptHoverEvents(True)
# Define the Hover Event
def hoverEnterEvent(self, event):
print(self.name+" hover enter")
self.setBrush(self.hover_brush)
self.update()
super().hoverEnterEvent(event)
# Define the Hover Leave Event
def hoverLeaveEvent(self, event):
print(self.name+" hover leave")
self.setBrush(self.fill_brush)
self.update()
super().hoverLeaveEvent(event)
# Main
if __name__ == "__main__":
# Initialize the app
app = QtWidgets.QApplication(sys.argv)
# Create the scene
scene = QtWidgets.QGraphicsScene()
# Create rectangles
central_rect = HoverRectItem(0, 0, 100, 50, parent = None, name = 'central')
scene.addItem(central_rect)
central_rect.setFlags(QtWidgets.QGraphicsItem.ItemIsMovable)
input_rect = HoverRectItem(-30, 0, 20, 50, parent = central_rect, name = 'input')
scene.addItem(input_rect)
output_rect = HoverRectItem(110, 0, 20, 50, parent = central_rect, name = 'output')
scene.addItem(output_rect)
# Create the view and show
view = QtWidgets.QGraphicsView(scene)
view.setWindowTitle("QGraphicsItem Hover Example")
view.show()
app.exec_()
output_rect.parent(). For the same reason, you should not add a child to the scene, as it's implicitly added when the parent is (check the output in a terminal or prompt and you'll see a warning). That said, graphics scene hover events follow the (QHoverEvent logic. If you only want to react when a hover event is exactly within the item boundaries, then simply check if theevent.pos()is within theboundingRect().hoverMoveEvent.