qt 4.7 removeChild() 和内存

发布于 2024-11-26 11:43:01 字数 606 浏览 7 评论 0原文

我正在编写一个 C++ qt 应用程序。在一部分中,我正在创建一个 QTreeWidget。用户可以在该树中创建和删除条目。 因此,当用户创建一个项目时,他将调用一个函数,然后该函数本身会调用:

QTreeWidgetItem* newItem = new QTreeWidgetItem();

当用户稍后决定删除该条目时,他会调用一个函数,然后该函数本身会调用:

QTreeWidgetItem* curItem = _ui->qTreeWidget->currentItem();
QTreeWidgetItem* parent = curItem->parent();
parent->removeChild(curItem);

现在对我来说擦除的问题是:内存怎么样该物品被占用? Qt 4.7 文档对removeChild的说法如下:

void QTreeWidgetItem::removeChild (QTreeWidgetItem * child) 删除子项指定的给定项目。删除的项目不会被删除。

那么如何删除孩子呢?

预先非常感谢! 唐尼

Im writing a c++ qt application. In one part I'm creating a QTreeWidget. The user has all possibilities to create and delete entries in that tree.
So when the user creates a item he will call a function which then itself calls:

QTreeWidgetItem* newItem = new QTreeWidgetItem();

When the user later decides to delete the entry, he calls a function which then itself will call:

QTreeWidgetItem* curItem = _ui->qTreeWidget->currentItem();
QTreeWidgetItem* parent = curItem->parent();
parent->removeChild(curItem);

The question that erases for me is now: what about the memory this item occupied? What The Qt 4.7 doc says to removeChild is the following:

void QTreeWidgetItem::removeChild ( QTreeWidgetItem * child )
Removes the given item indicated by child. The removed item will not be deleted.

So how do i delete a child?

Thanks a lot in advance!
Donny

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风启觞 2024-12-03 11:43:01

怎么样

delete curItem;

根据 文档,析构函数将删除该项目从包含它的树中删除,所以我认为您甚至不需要事先执行 removeChild

How about

delete curItem;

?

According to the documentation, the destructor will remove the item from the tree in which it is included, so I think you don't even need to perform the removeChild beforehand.

请你别敷衍 2024-12-03 11:43:01

通常,您只需在正确的时间删除对象:

delete curItem;

删除只是从父对象中删除其链接。

或者安排稍后删除

curItem->deleteLater();

You would normally just delete your object at the right time with:

delete curItem;

The remove is just removing its link from the parent.

or perhaps schedule it for deletion later:

curItem->deleteLater();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文