[PyKDE] QObject.insertChild problems

Phil Thompson phil at riverbankcomputing.co.uk
Sat Jul 5 21:16:01 BST 2003


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.

Phil




More information about the PyQt mailing list