[PyQt] Completely removing items from a layout
Lee Harr
missive at hotmail.com
Sat Nov 28 04:44:43 GMT 2009
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)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
win = MainWindow()
win.show()
app.exec_()
# Thanks for any assistance
_________________________________________________________________
Windows Live Hotmail: Your friends can get your Facebook updates, right from Hotmail®.
http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_4:092009
More information about the PyQt
mailing list