PyQt How to remove a layout from a layout

后端 未结 2 1413
南方客
南方客 2020-12-20 09:06
datainputHbox = QHBoxLayout()
layout = QVBoxLayout(self)
layout.addLayout(datainputHbox)


pagedatainputdeletboxbutton1.clicked.connect(lambda:self.boxdelete(datainp         


        
2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-20 09:54

    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

    Global Function

    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())
    

    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
    

提交回复
热议问题