PyQt 如何从布局中删除布局 [英] PyQt How to remove a layout from a layout
本文介绍了PyQt 如何从布局中删除布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
datainputHbox = QHBoxLayout()
layout = QVBoxLayout(self)
layout.addLayout(datainputHbox)
pagedatainputdeletboxbutton1.clicked.connect(lambda:self.boxdelete(datainputHbox))
def boxdelete(self, box):
这是 PyQt 程序如何编写 boxdelete 函数以删除 datainputHbox 表单布局.我尝试了很多.但是我只能删除所有小部件但不能删除布局.
This is the PyQt proragm How write boxdelete funtion in order to remove datainputHbox form layout. I try a lot of. However I just can remove all the widgets but cannot remove layout.
推荐答案
作为通用答案:取自这里 有轻微但重要的变化:你不应该调用widget.deleteLater().至少在我的情况下,这导致 python 崩溃
As a generic answer: taken from here with slight, but important changes: you should not call widget.deleteLater(). At least in my case this caused python to crash
全局函数
def deleteItemsOfLayout(layout):
if layout is not None:
while layout.count():
item = layout.takeAt(0)
widget = item.widget()
if widget is not None:
widget.setParent(None)
else:
deleteItemsOfLayout(item.layout())
连同来自 Brendan Abel 的答案 的 boxdelete 函数
together with the boxdelete function from Brendan Abel's answer
def boxdelete(self, box):
for i in range(self.vlayout.count()):
layout_item = self.vlayout.itemAt(i)
if layout_item.layout() == box:
deleteItemsOfLayout(layout_item.layout())
self.vlayout.removeItem(layout_item)
break
这篇关于PyQt 如何从布局中删除布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文