最近在重构以前写过的软件,遇到一个困扰了我两天的问题。今晚终于发现问题所在并解决,爽~
这个bug出现在:当我使用qt的GraphicsView Framework时,实例化了一个继承自QGraphicsPathItem的对象GraphicsPathItem,我在GraphicsPathItem类内重写了sceneEvent方法,来处理鼠标事件,以画出我想要的形状。但我发现,鼠标事件事件始终不会进入sceneEvent方法中,而在我使用的继承自QGraphicsLineItem类的对象中却工作正常。反复调试,始终找不到问题出在哪里,求助google也无果,没办法,只能看qt的源码。
在QGraphicsScence的mousePressEventHandler方法中,有这么一段代码
if (cachedItemsUnderMouse.isEmpty()) {
cachedItemsUnderMouse = itemsAtPosition(
mouseEvent->screenPos(),
mouseEvent->scenePos(),
mouseEvent->widget());
}
而我的GraphicsPathItem是这样定义的:
penItem = new GraphicsPathItem(QPainterPath(mouseEvent->scenePos()));<