[PyKDE] Dynamic layouts

Boudewijn Rempt boud at rempt.xs4all.nl
Mon Mar 19 21:14:18 GMT 2001


On Saturday 17 March 2001 02:57, you wrote:
dding is easy. Take a look at the following script:
>
> Have you tried using Qobject.removeChild()?
>
>

Yes - I've tried:

* removing all Python references 
* using removeChild: QObject.removeChild() has an invalid type
* using close(TRUE) on the widget: AttributeError: close
* deleteAllWidgets(): gives QGList::locate: Index 0 out of range

And I've tried to iteratate through the layoutitems, but that didn't work, 
because next() doesn't seem to be implemented. In general, destroying widgets 
is quite hard in PyQt, I feel.

Here's the script again:

#
# 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)

        self.buttons = []

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



    def slotRemoveWidget(self):
        print "remove"
        w=self.buttons[-1:]
        del self.buttons[-1:]
        self.removeChild(w) # does not remove widget from screen, error, too
        #w.close(1) # AttributeError: close
        #self.widgetLayout.deleteAllItems() : QGList::locate: Index 0 out of 
range
        
            
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)


-- 

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




More information about the PyQt mailing list