「 QT 避坑指南 」 远离那些趟过无数次的坑
整理 | 猿胖子
出品 | 猿武场(ID:apesarena)
关注公众号并回复数字「 1024 」加入猿武场微信社群
QLayoutItem *child;
while ((child = layout->takeAt(0)) != 0) {
//删除 child
layout->removeWidget(child->widget());
delete child;
}
然鹅并未达到我们的预期,child 仅是移出布局实际并未删除。
官方解释
Removes the widget widget from the layout. After this call, it is the caller's responsibility to give the widget a reasonable geometry or to put the widget back into a layout or to explicitly hide it if necessary.
Note: The ownership of widget remains the same as when it was added.
机器翻译:从布局中删除小部件。在这个调用之后,调用者有责任给小部件一个合理的几何图形,或者将小部件放回布局中,或者在必要时显式地隐藏它。
注意:小部件的所有权与添加时相同。

正确的方式
在 removeWidget 之后重置其标志位,然后在删除
QLayoutItem *child;
while ((child = layout->takeAt(0)) != 0) {
//删除 child
layout->removeWidget(child->widget());
//重点 - 敲黑板
child->widget()->setParent(0);
delete child;
}

注公众号并回复数字「 1024 」加入猿武场微信社群
欢迎加入程序员社群,更多技术摘要等你拿走
社群福利:
1. 行业大牛技术手札,知识点汇总;
2. 求职/招聘信息内推;
4. 人际交往,增强技术宅人际交流;
5. 调节繁杂无趣的闲暇时光;
6. 不定期线上周边於线下技术活动沙龙。

文章转载自猿武场,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




