QWidget 在销毁时是否会将其自身从 GUI 中删除?

发布于 2024-11-07 15:55:09 字数 97 浏览 0 评论 0原文

如果我使用 delete 删除 QWidget,它是否会从 GUI 中取消注册,还是我必须手动执行此操作?这种行为有逻辑原因吗?

If I delete a QWidget using delete, does it unregister itself from the GUI or do I have to do this manually? Is there a logical reason for this behavior?

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

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

发布评论

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

评论(2

小糖芽 2024-11-14 15:55:09

当您在布局或堆叠小部件上调用 addWidget 时,小部件的所有权将转移到布局/堆叠小部件。所有这意味着,如果布局/堆叠小部件被删除,那么添加到其中的所有小部件也会被删除。

一旦您使用完某个小部件,就完全可以将其删除无论是谁拥有它。所有权只是清理对象层次结构内存的一种便捷方法。它绝不表示拥有它的对象必须删除它。如果是这种情况,那么一旦您添加了所有小部件,您将只能删除全部或根本不删除它们!

如果您不希望在布局/堆叠小部件被删除时删除您的小部件,那么您可以调用 removeWidget。请注意,目前尚不清楚该小部件的所有权真正去向何处。一个简单的测试应用程序。我刚刚写的建议 removeWidget 甚至根本没有从 QStackedWidget 转移所有权!

因此,为了回答您的问题,如果您删除它,Qt 将正确地从布局/堆叠小部件中删除该小部件。此外,如果小部件不再属于布局/堆叠小部件,那么这是删除小部件的正确方法。

When you call addWidget on a layout or stacked widget the ownership of the widget is transferred to the layout/stacked widget. All this means is that if the layout/stacked widget gets deleted then all the widgets that were added to it get deleted too.

It's perfectly okay to delete a widget once you are finished with it regardless of who owns it. The ownership is simply a convenient way of clearing up the memory of a hierarchy of objects. It in no way says that the object that owns it must delete it. If that were the case then once you added all your widgets you would only be able to get rid of them all or none at all!

If you didn't want your widget to be deleted when the layout/stacked widget gets deleted then you would call removeWidget. Note that it's not clear where the ownership of the widget really goes. A simple test app. I just wrote suggests that removeWidget did not even transfer ownership away from a QStackedWidget at all!

So, to answer your question, Qt will correctly remove the widget from the layout/stacked widget if you delete it. Furthermore this is the correct way to remove the widget if it no longer belongs in the layout/stacked widget.

往日情怀 2024-11-14 15:55:09

正如 @CatPlusPlus 已经指出的,Qt 使用所有权系统。因此,每当您将小部件添加到布局或将布局添加到小部件等时,被添加者的所有权都会赋予加法器/父级。这通常记录在方法文档中。例如,如果您查看 QWidget::addLayout(QLayout*) 的文档,它会说 Qwidget 拥有 QLayout 的所有权。当您删除父级时,它也会删除其所有子级。
阅读这篇文章了解更多信息。

Qt 中的对象树和所有权

这个方法非常有用,因为在传统的C++ 开发人员必须跟踪堆上分配的每一位内存。然而,这种所有权制度要求开发商只跟踪父母。

As already pointed out by @CatPlusPlus , Qt uses an ownership system. So whenver you add widgets to layouts or layouts to widgets and so forth the ownership of the addee is given to the adder/parent. This is usually documented in the method documentation. For example if you look at documentation for QWidget::addLayout(QLayout*), it says the Qwidget takes ownership of the QLayout. When you delete the parent it deletes all its children as well.
Read this article for more info .

Object Trees and Ownership in Qt

This method is very useful, because in traditional C++ the developer has to keep track of every bit of memory allocated on the heap. This ownership system however requires that the developer onl keep track of the parents.

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