【发布时间】:2012-03-11 13:22:09
【问题描述】:
我试图找到可以采用 qt 布局并从中删除所有内容的东西。只是想象一下窗口的样子 - 我有:
QVBoxLayout
| ------QHboxLayout
|---------QWidget
| ------QHboxLayout
|---------QWidget
.........
所以我需要一些可以递归调用的东西来清除和删除我父母 QVBoxLayout 的所有东西。我尝试了这里提到的东西(Clear all widgets in a layout in pyqt),但它们都不起作用(无论如何都没有标记正确的答案)。我的代码如下所示:
def clearLayout(self, layout):
for i in range(layout.count()):
if (type(layout.itemAt(i)) == QtGui.QHBoxLayout):
print "layout " + str(layout.itemAt(i))
self.clearLayout(layout.itemAt(i))
else:
print "widget" + str(layout.itemAt(i))
layout.itemAt(i).widget().close()
但它给出了一个错误:
layout.itemAt(i).widget().close()
AttributeError: 'NoneType' object has no attribute 'close'
=>编辑
这有点工作(但如果有任何其他Layout 而不是HBoxLayout:
def clearLayout(self, layout):
layouts = []
for i in range(layout.count()):
if (type(layout.itemAt(i)) == QtGui.QHBoxLayout):
print "layout " + str(layout.itemAt(i))
self.clearLayout(layout.itemAt(i))
layouts.append(layout.itemAt(i))
else:
print "widget" + str(layout.itemAt(i))
if (type(layout.itemAt(i)) == QtGui.QWidgetItem):
layout.itemAt(i).widget().close()
【问题讨论】: