[PyKDE] QCanvasSprite and QPixmapArray scope issues
Phil Thompson
phil at riverbankcomputing.co.uk
Fri Apr 28 13:48:25 BST 2006
On Friday 28 April 2006 1:27 pm, Alexander Borghgraef wrote:
> Hi all,
>
> I'm writing an interface for displaying and processing sequences of
> images. I'm using QCanvasSprites to
> contain and animate the sequences. My first implementation worked, but when
> I cleaned it up a bit, I got
> segmentation faults. The problem appeared to be that QCanvasSprite takes a
> QPixmapArray for parameter,
> but that the sprite doesn't seem to manage the array's memory. So when I
> wrote a gui class a bit like this:
>
> class MyGui( QMainWindow ):
> def __init__( self ):
> ...
> self.canvas = QCanvas( 200, 200 )
> self.seqView = QCanvasSprite( QPixmapArray( listOfQPixmap ),
> self.canvas )
>
> I got a segmentation fault as soon as the program exited MyGui.__init__,
> most likely the first time the gui tried to
> refresh the sprite. Same thing with:
>
> ...
> pixArray = QPixmapArray( listOfQPixmap ),
> self.seqView = QCanvasSprite( pixArray , self.canvas )
>
> What fixed it was this:
> ...
> self.pixArray = QPixmapArray( listOfQPixmap ),
> self.seqView = QCanvasSprite( self.pixArray , self.canvas )
>
> Why is this? Logically, QCanvasSprite should hold a pointer towards the
> pixmap array, so even if the variable
> goes out of scope, the sprite should still point to the array, shouldn't
> it. It works that way with QCanvas: you
> can for example do this:
>
> self.canvas = QCanvas( 200,200 )
> QCanvasRectangle( 20, 20, 30, 40, self.canvas ).show()
>
> While the QCanvasRectangle goes out of scope, it still remains accessible
> through the self.canvas variable: it
> remains visible and you can access it for instance through
> QCanvas.allItems()[0].
> Why doesn't QCanvasSprite
> follow this logic? Is my third example the right way of using the class, or
> are there better options? Or did I miss
> something obvious? I'm using python 2.3, Qt 3.3.3, and PyQt 3.13
QCanvasSprite does not take responsibility for calling the QPixmapArray dtor,
ie. it doesn't take ownership of the QPixmapArray instance. Therefore, when
the latter goes out of scope it is garbage collected (unless there is another
reference to it). Your solution is the conventional way of handling it.
Python isn't C++.
Phil
More information about the PyQt
mailing list