[PyKDE] QObject.insertChild problems

Austin Clements amdragon at mit.edu
Sun Jul 6 00:11:01 BST 2003


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?

> Phil

-- 
        __
       /\ \  Austin Clements
      /  \ \  Somewhere in the dream we call reality you will find me,
     / /\ \ \  searching for the reality we call dreams.
    / / /\ \ \
   / / /__\_\ \  http://www.mit.edu/~amdragon
  / / /_______ \  amdragon at mit.edu
 / / ________/\ \  ICQ: 6564732
/ / /        \ \ \  DC2.DwGmL3mW-T-PahltvwSkl,bsCor,eblB-Av++N^
\/_/          \_\/   FM---H+$-R+++!Ac+++U?I--#JS+V+++Q+++Tc+++!E




More information about the PyQt mailing list