我在pyqt中遇到布局问题。如果layout.count()返回旧项目计数,则仍然关闭布局中的项目。所以我认为.close()并没有真正从布局中删除项目。这是一个完整的工作示例。
import sys
from PyQt4 import QtGui,QtCore
class LayoutTest(QtGui.QWidget):
def __init__(self):
super(LayoutTest, self).__init__()
self.vvbox = QtGui.QVBoxLayout()
self.dvbox = QtGui.QVBoxLayout()
vbox = QtGui.QVBoxLayout()
vbox.addLayout(self.vvbox)
vbox.addLayout(self.dvbox)
self.setLayout(vbox)
self.add_button = QtGui.QPushButton("Add Items")
self.edit_button = QtGui.QPushButton("Remove Items")
self.chk_button = QtGui.QPushButton("Check Items")
self.vvbox.addWidget(self.add_button)
self.vvbox.addWidget(self.edit_button)
self.vvbox.addWidget(self.chk_button)
self.connect(self.add_button, QtCore.SIGNAL("clicked()"), self.addButtons)
self.connect(self.edit_button, QtCore.SIGNAL("clicked()"), self.removeButtons)
self.connect(self.chk_button, QtCore.SIGNAL("clicked()"), self.checkItems)
self.setGeometry(300, 200, 400, 300)
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Escape:
self.close()
def addButtons(self):
for i in range(0, 5):
self.r_button = QtGui.QPushButton("Button %s " % i)
self.dvbox.addWidget(self.r_button)
def removeButtons(self):
for cnt in range(self.dvbox.count()):
self.dvbox.itemAt(cnt).widget().close()
def checkItems(self):
QtGui.QMessageBox.information(self, 'Count',"You have %s Items in Layout" % self.dvbox.count(), QtGui.QMessageBox.Ok)
def run():
app = QtGui.QApplication(sys.argv)
ex = LayoutTest()
ex.show()
sys.exit(app.exec_())
if __name__ == "__main__":
run()
只需在添加按钮中单击两次,然后删除按钮。然后只需检查项目。关闭后,您将获得布局中的n个项目。
那么从关闭以外的布局中删除小部件的最佳方法是什么?
答案 0 :(得分:7)
您的评论确实是一种解决方案,而不是close
使用deleteLater
。它更安全。通过一些修改,我将您的方法重写为:
def removeButtons(self):
for cnt in reversed(range(self.dvbox.count())):
# takeAt does both the jobs of itemAt and removeWidget
# namely it removes an item and returns it
widget = self.dvbox.takeAt(cnt).widget()
if widget is not None:
# widget will be None if the item is a layout
widget.deleteLater()