[PyKDE] QObject.insertChild problems

Phil Thompson phil at riverbankcomputing.co.uk
Sun Jul 6 10:55:01 BST 2003


On Saturday 05 July 2003 11:10 pm, Austin Clements wrote:
> On Jul 05 at  8:15 pm, Phil Thompson handed me the following bytes:
> > On Saturday 05 July 2003 10:39 am, Austin Clements wrote:
> > > 'Lo.  I am implementing a widget that has to layout child widgets in a
> > > particular way (similar to a QVBox, but not quite), so I tried
> > > overriding the insertChild method, but found that the objects passed
> > > to it were always the underlying QObject objects instead of the actual
> > > widgets I added to it.  Here's a simplification of my code and
> > > its output:
> > >
> > >
> > > import sys
> > > from qt import *
> > >
> > > class ComponentStack(QWidget):
> > >     def __init__(self, parent, name="Component stack"):
> > >         QWidget.__init__(self, parent, name)
> > >         self.__vbox = QVBoxLayout(self)
> > >
> > >     def insertChild(self, obj):
> > >         print "insertChild(%s)" % `obj`
> > >         QWidget.insertChild(self, obj)
> > >         try:
> > >             self.__vbox.add(obj)
> > >         except AttributeError: pass
> > >
> > > app = QApplication(sys.argv)
> > > cs = ComponentStack(None)
> > > app.setMainWidget(cs)
> > > QPushButton("Button", cs)
> > > cs.show()
> > > app.connect(app, SIGNAL('lastWindowClosed()'), app, SLOT('quit()'))
> > > app.exec_loop()
> > >
> > >
> > > Output:
> > > insertChild(<qt.QObject instance at 0x81e39ac>)
> > > insertChild(<qt.QObject instance at 0x81e49b4>)
> > > Traceback (most recent call last):
> > >   File "bug.py", line 13, in insertChild
> > >     self.vbox.add(obj)
> > > TypeError: Argument 1 of QLayout.add() has an invalid type
> > >
> > >
> > > The first call occurs in the QWidget constructor, before I've created
> > > the vbox, but the next one, when I add the QPushButton, is the
> > > problem.  Am I doing something wrong, or is there a better way to do
> > > this?
> >
> > I think the problem is that the QPushButton is not fully formed
> > (ie. it's ctor hasn't yet completed) by the time it is being passed
> > to its parent's insertChild().
> >
> > A better approach should be to detect the new child by looking for
> > QChildEvents which won't be received until the child is fully
> > formed.
>
> Thanks; that did the trick.  Though this brings up another problem
> (it's okay if this isn't answerable or fixable, but it would be nice
> to know why it happens): If the child event is a removal, then the
> child specified in the event is the QObject, not the QWidget.  Is this
> just another artifact of the event being generated within the object's
> destructor?

I don't think so as it is where the event is being processed that matters - 
not where the event is being generated.

Phil




More information about the PyQt mailing list