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哦

  • 31
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值