[PyKDE] drawing on QCanvas not working
phil at river-bank.demon.co.uk
Tue May 8 06:25:48 BST 2001
Peter Hinely wrote:
> Phil Thompson wrote:
> > Remember that sometimes you need to keep explicit references to widgets
> > to stop them from being garbage collected. (Hint: item => self.item,
> > newCanvas => self.newCanvas.)
> It works now. Thank you very much. That was my first python program.
> But I'm quite confused by this behavior. Is it some artifact brought on
> by the interface between Python and C++ objects? Surely pure Python would
> not behave this way, right?
> should have prevented the QCanvasView from being destructed because the
> main window maintains a reference to the canvasView.
> QCanvasView(self.newCanvas, self)
> should have prevented the canvas object from being destructed because the
> QCanvasView maintains a reference to the canvas.
> QCanvasEllipse(20, 20, self.newCanvas)
> should have prevented the ellipse from being destructed.
> It's not clear to me why I need to maintain an explicit reference to these
> objects in my app.
There is a difference between keeping a reference and taking
responsibility for calling an object's dtor - it's the latter that is
important. setCentralWidget() keeps a reference but does not take the
responsibility for calling the dtor - therefore it is left to the Python
Most of the time this is not a problem - when a QObject is added as a
child of a parent QObject, the parent takes reponsibility for the child
so you don't have to keep an explicit reference to the child yourself.
This covers 95% of cases. Unfortunately QCanvas and QCanvasItem don't
follow this model - which I think is a design bug. (I often think that
QCanvas was written by somebody else and adopted by Trolltech.)
More information about the PyQt