重载QGraphicsItemGroup和QGraphicsItem后,将QGraphicsItem加入到QGraphicsItemGroup后,发现QGraphicsItem中的mousePressEvent,mouseMoveEvent,mouseReleaseEvent全部无法获取,实际接受消息对象为QGraphicsItemGroup。
查找发现有一个相关ISSUES,QTBUG-15477,地址:https://bugreports.qt-project.org/browse/QTBUG-15477,根据文章说明,在新的QGraphicsItem中重载sceneEvent函数后运行正常。
相关代码:
bool BaseItem::sceneEvent(QEvent *event)
{
if (event->type() == QEvent::GraphicsSceneMousePress)
{ mousePressEvent((QGraphicsSceneMouseEvent *)(event)); }
else if (event->type() == QEvent::GraphicsSceneMouseRelease)
{ mouseReleaseEvent((QGraphicsSceneMouseEvent *)(event)); }
else if (event->type() == QEvent::GraphicsSceneMouseMove)
{ mouseMoveEvent((QGraphicsSceneMouseEvent *)(event)); }
event->setAccepted(true);
return true;
}