Lee Harr wrote:
I wrote about this a couple of weeks ago, but I did not have a small
working example to show the problem I'm having. Now I do.


I have a layout created in Qt4 Designer which I fill with a series of
complex layouts at runtime. That works great. Later on, I want
to completely remove all of them and fill it up again with a
different set.

The re-filling up part works fine, but the original set of items is
still visible and I can't work out how to get rid of them completely.


The code looks something like the following. Resize the window and
you will see a blob that is the leftovers from the first call to fillup()



import sys
from PyQt4 import QtGui

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
        MainWindow.setCentralWidget(self.centralwidget)


class L2(QtGui.QHBoxLayout):
    # Represents a more complex layout to be placed inside verticalLayout
    def __init__(self, name):
        QtGui.QHBoxLayout.__init__(self)
        nm = QtGui.QLabel(name)
        self.addWidget(nm)


class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.fillup()
        self.unfill()
        self.fillup()

    def fillup(self):
        for name in [str(n) for n in range(10)]:
            txt = name*20
            self.ui.verticalLayout.addLayout(L2(txt))

    def unfill(self):
        # This is the troublesome one.
        # How do I get rid of the contents of the layout completely?

        vl = self.ui.verticalLayout
        while vl.count():
            item = vl.itemAt(0)
            vl.removeItem(item)

you need to delete both the layouts and their contents:

    def unfill(self):
        def deleteItems(layout):
            if layout is not None:
                while layout.count():
                    item = layout.takeAt(0)
                    widget = item.widget()
                    if widget is not None:
                        widget.deleteLater()
                    else:
                        deleteItems(item.layout())
        deleteItems(self.ui.verticalLayout)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    win = MainWindow()
    win.show()
    app.exec_()
_______________________________________________
PyQt mailing list    [email protected]
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to