Remove all items from a layout

后端 未结 2 1570
名媛妹妹
名媛妹妹 2020-11-27 08:03

I was trying to find something that would take a qt layout and delete everything from it. Just to imagine what the window looks like - I have:

QVBoxLayout
          


        
2条回答
  •  北海茫月
    2020-11-27 08:21

    The safest way to clear a layout is to extract the items with its takeAt method, and then explicitly delete any widgets with deleteLater:

    def clearLayout(self, layout):
        if layout is not None:
            while layout.count():
                item = layout.takeAt(0)
                widget = item.widget()
                if widget is not None:
                    widget.deleteLater()
                else:
                    self.clearLayout(item.layout())
    

提交回复
热议问题