Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
638 views
in Technique[技术] by (71.8m points)

python - PyQt How to remove a layout from a layout

datainputHbox = QHBoxLayout()
layout = QVBoxLayout(self)
layout.addLayout(datainputHbox)


pagedatainputdeletboxbutton1.clicked.connect(lambda:self.boxdelete(datainputHbox))

def boxdelete(self, box):

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...