现在要做一个需要在父item上添加控制点(子item)来调整父item形状的功能。
关键是子item移动之后,要把自己的位置信息交给父item,让父item更新自己的形状。
查看了很久Qt的document,需要在父ITEM中用setFiltersChildEvents(true)来设置允许拦截所有给子item的消息,然后再重载这个函数:
bool sceneEventFilter ( QGraphicsItem * watched, QEvent * event )
{
if(watched->type()==QGraphicsRectItem::Type) //我的子item都是rect
{
if(event->type() == QEvent::GraphicsSceneMouseMove)
{
//处理移动消息
}
else if(event->type() == QEvent::GraphicsSceneMouseRelease)
{
//处理鼠标释放消息
}
}
return false; //false的话子item的消息仍然要被处理,如果返回true,子item的消息将不再被处理}
解决了这个问题。
重载时要注意判断是否是自己想要拦截的item。
或者也可以使用
installSceneEventFilter
来设置允许拦截某item的消息,指定要过滤的item参考文章:http://blog.csdn.net/fly542/article/details/6459661