从 QVBoxLayout 中删除自定义小部件
我有一个带有 QVBoxLayout 的 QFrame,并且我正在将自己的自定义小部件添加到布局中 模拟 QListWidget 但在项目中具有更多信息/功能。我将小部件添加到布局中,并在成员变量中保留引用(这是Python):
self.sv_widgets[purchase.id] = widget
self.vl_seatView.addWidget(widget)
然后,当我完成一个项目时,我想将其从屏幕上删除并清理引用:
self.vl_seatView.removeWidget(self.sv_widgets[purchase.id])
del self.sv_widgets[purchase.id]
不幸的是,小部件仍然是正在屏幕上显示!我已经检查过,只将其添加到布局中一次(实际上只显示一份副本),尝试在布局上手动调用 update() ,但无济于事。这样做的正确方法是什么?
I've got a QFrame with a QVBoxLayout and I'm adding my own custom widgets to the layout
to simulate a QListWidget but with more information/functionality in the items. I add the widget to the layout and keep a reference in a member variable (this is Python):
self.sv_widgets[purchase.id] = widget
self.vl_seatView.addWidget(widget)
Then when I'm done with an item I want to remove it from the screen and clean up the reference:
self.vl_seatView.removeWidget(self.sv_widgets[purchase.id])
del self.sv_widgets[purchase.id]
Unfortunately, the widget is still being displayed on the screen! I've checked and I'm only adding it to the layout once (and indeed only one copy is displayed), tried manually calling update() on the layout, but to no avail. What's the right way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以这样做:
sip.delete(obj)
显式调用相应 C++ 对象的析构函数。removeWidget
不会导致调用此析构函数(它仍然此时有一个父对象),而del
仅将 Python 对象标记为垃圾回收。您可以通过执行以下操作来实现相同的目的(可能更干净):
You can do this:
sip.delete(obj)
explicitely calls the destructor on the corresponding C++ object.removeWidget
does not cause this destructor to be called (it still has a parent at that point) anddel
only marks the Python object for garbage collection.You can achieve the same by doing (propably cleaner):
您还可以使用 self.sv_widgets[purchase.id].deleteLater()
You can also use self.sv_widgets[purchase.id].deleteLater()