Qt在QGraphicsScene中removeItem后delete发生的崩溃以及显示残留

问题描述

自己利用qt的graphicsview框架实现自定义item的创建和交互的过程中,需要移除不需要的自定义item,但在自定义item利用重新实现的旋转和缩放交互后,再进行removeItem()+delete,就偶尔会发生访问非法内存的错误,而且就是只使用removeItem(),view中也偶尔会残留item。

解决方式

使用QGraphicsScene的setItemIndexMethod(QGraphicsScene::NoIndex);
禁用索引的快速查询,即可解决上述问题。

其他

对于使用removeItem()后是否还需要delete的问题,官方问题有给出明确解释

Removes the item item and all its children from the scene. The ownership of item is passed on to the caller (i.e., QGraphicsScene will no longer delete item when destroyed).
从场景中remove该item及其所有子item。 item的所有权将传递给调用者(即,QGraphicsScene在销毁时将不会删除item)。

所以new了记得delete哦

猜你喜欢

转载自blog.csdn.net/ko1234634/article/details/115217250