有没有办法删除 QGridLayout 中的 QWidget? [英] Is there any way to remove a QWidget in a QGridLayout?

查看:306
本文介绍了有没有办法删除 QGridLayout 中的 QWidget?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的是完全删除一个小部件(清除它,删除它等等...),但它在网格布局中,所以即使在调用 removeWidget 时,它仍然保留一个指针,所以python不想删除对象.这是(剥离的)代码:

What I want is to completely remove a widget (purge it, delete it, etc...), but it's in a gridlayout, so even when calling removeWidget, it still keeps a pointer, so python doesn't want to remove the object. Here is the (stripped) code:

def addRow(self, row):
    self.entries.insert(row, QtGui.QLineEdit())
    self.gridlayout.addWidget(self.entries[row], row, 0)
...
def remRow(self, row):
    self.gridlayout.removeWidget(self.entries[row])
    del(self.entries[row])
...
(in another function)
foo.addRow(0)
foo.remRow(0)

它从网格布局中删除了小部件,但并没有完全删除它,所以它实际上被打包在(?)布局下面,并且小部件显然比布局大(虽然不确定,因为我看不到结束).

It removes the widget from the gridlayout, but it doesn't remove it completely, so it actually gets packed beneath(?) the layout, and the widget is apparently bigger than the layout (not sure though, as I cannot see the end).

再说一次,有没有办法完全删除 QGridLayout 内的小部件?

So again, is there any way to completely remove a widget that was inside a QGridLayout?

提前致谢!

推荐答案

布局将小部件重新设置为容器的父级.因此,即使您从布局中删除它,容器小部件仍然是父级,因此该小部件不会被删除.您应该调用 .deleteLater() 来告诉 Qt 删除该小部件(有效清除该小部件的所有引用,以便 Python 可以清除它):

Layouts re-parent the widgets to the containers. So even if you remove it from the layout, container widget is still the parent, so that widget is not removed. You should call .deleteLater() to tell Qt to get rid of that widget (effectively clearing all references for the widget so that Python can purge it):

def remRow(self, row):
    self.gridlayout.removeWidget(self.entries[row])
    self.entries[row].deleteLater()
    del self.entries[row]

这篇关于有没有办法删除 QGridLayout 中的 QWidget?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆