pyqt:如何删除小部件?
我有一个 QGroupBox
小部件,其中有我想要删除的子部件。我该怎么做?我在 文档。我只能看到如何从布局中删除内容,但显然,这并没有从实际的小部件中删除它。
I have a QGroupBox
widget with children in it that I want to remove. How do I do that? I can't find any removeWidget
, removeChild
, removeItem
, or anything similar in the docs. I can only see how to remove things from a layout, but that, apparently, doesn't remove it from the actual widget.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的小部件没有依赖于它的子小部件,我认为您可以使用:
根据我的测试,当小部件有子部件时,您必须:
如果您在类或全局级别没有小部件的变量名称,则您可以使用仍然可以使用
layout.takeAt(index)
从布局中删除,并从该函数使用QLayoutItem.widget()
返回的QLayoutItem
获取小部件指针方法,在这种情况下你不需要将变量名称分配给None
,因为它不会在函数外部被引用。尝试这两种方法,看看哪种方法适合您(重复多次后不会泄漏内存)。
If your widget have no child widgets that depend on it I think you can use:
according to my tests, when the widget has children, you have to:
If you don't have a variable name for the widget at class or global level you still can remove from layout with
layout.takeAt(index)
and get the widget pointer from theQLayoutItem
this functions returns withQLayoutItem.widget()
method, in that case you don't need to assign toNone
the variable name because it is not referenced outside your function.Try both methods and see what works for you (don't leak memory after repeating a good number of times).
好吧,这可行:在要删除的小部件上,调用
widget.setParent(None)
。我喜欢添加到布局中如何将小部件添加到容器中,但从布局中删除并不......有趣的东西。Well, this works: on the widget you want to remove, call
widget.setParent(None)
. I like how adding to a layout adds a widget to the container, but removing from a layout doesn't... fun stuff.