Qt 图形视图框架 QGraphicsSceneBspTree出现的崩溃问题
现象
最近在处理了Qt视图中的崩溃问题,删除动态图元宕机报pure virtual method called,debug定位问题出在了QGraphicsSceneFindItemBspTreeVisitor::visit()函数中
原因
问题在于item的boundingRect()是动态获取动态改变的图元,图元修改的BoundingBox时没有预先通知Scene,使得其通过Bsp树查找图元信息时出错导致的。
解决
动态改变图元大小时,加入prepareGeometryChange()告知
结论
If you want to change the item’s bounding rectangle, you must first
call prepareGeometryChange(). This notifies the scene of the imminent
change, so that it can update its item geometry index; otherwise, the
scene will be unaware of the item’s new geometry, and the results are
undefined (typically, rendering artifacts are left within the view).
由于在自定义图元移动或者boundingrect变化时,没有提前调用prepareGeometryChange(),导致场景内可能存在无效的item索引,在绘图过程中引发崩溃。
参考:
https://blog.csdn.net/xuebin5260/article/details/50477931
https://blog.csdn.net/smilexx21/article/details/88789315