[PyKDE] Dynamic layouts

Boudewijn Rempt bsarempt at xs4all.nl
Wed Mar 14 21:01:18 GMT 2001


On Wednesday 14 March 2001 07:16, I wrote:
> I haven't found a way to really delete individual widgets from a layout,
> yet, but as long as you keep an explicit reference to the middle layout
you
> can call self.layout.deleteAllItems() and then add new items, I think.
>
> I haven't tried it myself yet, but will play a bit. By coincidence, I'm
> just now writing up a bit on Qt layouts, so it's an interesting
question.
> ;-)

Well, I've been working on it today. Still no way to delete the widgets,
but
adding is easy. Take a look at the following script:

#
# layoyut.py - adding and removing widgets to a layout
#
import sys
from qt import *


class MainWindow(QMainWindow):

    def __init__(self, *args):
        apply(QMainWindow.__init__, (self, ) + args)
        self.setCaption("Adding and deleting widgets")
        self.setName("main window")
        self.mainLayout=QVBoxLayout(self, 5, 5, "main")
        self.buttonLayout=QHBoxLayout(self.mainLayout, 5, "button")
        self.widgetLayout=QVBoxLayout(self.mainLayout, 5, "widget")

        self.bnAdd=QPushButton("Add widget", self, "add")
        self.connect(self.bnAdd, SIGNAL("clicked()"), self.slotAddWidget)

        self.bnRemove=QPushButton("Remove widget", self, "remove")
        self.connect(self.bnRemove, SIGNAL("clicked()"),
self.slotRemoveWidget)
        self.buttonLayout.addWidget(self.bnAdd)
        self.buttonLayout.addWidget(self.bnRemove)

    def slotAddWidget(self):
        print "add"
        widget=QPushButton("test", self)
        self.widgetLayout.addWidget(widget)
        widget.show()

    def slotRemoveWidget(self):
        print "remove"
        it = self.widgetLayout.iterator()
        while (it.current()):
            c=it.current()
            w=c.widget()
            print c, w
            it.deleteCurrent()
            #it.next()
        it.deleteCurrent()

def main(args):
    app=QApplication(args)
    win=MainWindow()
    win.show()
    app.connect(app, SIGNAL("lastWindowClosed()")
                                 , app
                                 , SLOT("quit()")
                                 )
    app.exec_loop()

if __name__=="__main__":
    main(sys.argv)

Deleting the widgets is still impossible. I've tried to use
deleteAllWidgets(), but it didn't do anything. Iterating over the widgets
with it.next() and then deleting it doesn't work either. Firstly, next()
appears to be unavailable, as you can see when you try this script without
with it.next() and then deleting it doesn't work either. Firstly, next()
appears to be unavailable, as you can see when you try this script without
the hash before it.next() (put a hash before it.deleteCurrent()):

<qt.QLayoutItem instance at 0x834a1dc> None
Traceback (most recent call last):
  File "layout.py", line 41, in slotRemoveWidget
    it.next()
AttributeError: next

Anyway, getting the widget with it.current().widget() won't give you
the widget the layout item holds: it returns None.

I'm not entirely sure whether it's possible at all to delete the widgets
without deleting the parent. Currently, there are references to the
buttons generated by slotAddWidget in the main window and in the layout
manager, and I can't figure out how to break them all.

--
Boudewijn Rempt | http://www.valdyas.org






More information about the PyQt mailing list