PyQt How to remove a layout from a layout

后端 未结 2 1407
南方客
南方客 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:52

    You can remove QLayouts by getting their corresponding QLayoutItem and removing it. You should also be storing references to your Layouts, otherwise there is no other way to access them later on unless you know the widget they belong to.

    datainputHbox = QHBoxLayout()
    self.vlayout = QVBoxLayout(self)
    layout.addLayout(datainputHbox)
    pagedatainputdeletboxbutton1.clicked.connect(lambda: self.boxdelete(datainputHbox))
    
    def boxdelete(self, box):
        for i in range(self.vlayout.count()):
            layout_item = self.vlayout.itemAt(i)
            if layout_item.layout() == box:
                self.vlayout.removeItem(layout_item)
                return
    

提交回复
热议问题