[Kde-games-devel] KGameCanvas

Maurizio Monge maurizio.monge at gmail.com
Mon Oct 2 02:26:23 CEST 2006


(I'm replying to the list because this clarification could be useful for others)

Ouch, I think that i misunderstood what you where asking :)
I was thinking that you just wanted to remove the item from the canvas,
making it an "orphan" item, instead of some sort automatic deletion.
The "while" thing should work anyway, because when an item is destroyed
it is automatically removed from the abstract canvas (so the canvas does
not hold a dangling pointer). On the other side, when an abstract canvas
is destroyed, all its items are made "orphan" items (same as if
putInCanvas(NULL) was called for eachitem), so that they do not point
to a destroyed canvas, but they may be reused in another canvas (i don't
know if this is useful, but who know).
When i designed the canvas i did not put any memory management in it
to make the design more clear, but if you think that an "autoDeleteItems"
flag could be useful, it can be added with very little work.
Or, you can just do:
while ( ! items()->isEmpty() )
     delete items()->first();
note first instead of takeFirst. You should never modify the items
list out of the
canvas (to make design clear and keep things consistent), this is why you get
the constness error, but if you just simply delete an item it will be
automatically
removed from its list :)
About keeping a list of the items and calling putInCanvas(NULL) for each before
deletion, it is not required as you can see.

On 10/2/06, Mauricio Piacentini <mauricio at tabuleiro.com> wrote:
> > Ouch, i think the problem is that the documentation is not clear enough
> > about this :)
> > To put an item out and in an abstract canvas, you just have to call
> > Item::putInCanvas, doing so the item is removed from the current canvas,
> > and if the argument is not NULL it is also inserted in another canvas.
>
> I am sending this in private, since the discussion probably does not
> interest the whole list. BTW, the port of KMahjong to KGameCanvas has
> already started, during aKademy.
>
> But suppose the following scenario:
>
> a) Some game class creates maybe 100 pixmaps item (using new) and store
> them in the canvas.
>
> b) Later on I need the calling class to destroy all elements and create
> 100 new ones. Notice that the creating class might not be storing
> pointers to the elements, they are only in the canvas. I imagined that
> something like
>
> canvas->clearAll();
>
> Could take care of this, but maybe I am mistaken.
>
> I believe there is a need to have a way not only to remove the elements
> from the canvas, but also delete them, otherwise memory will leak. But
> maybe I am just tired from the trip back to Brazil.
>
> How would you handle this situation in a simple example? QGraphicsScene
> was a removeItem method, would you use it with the items() collection,
> perhaps? I also tried using something like
>
> while (items()->IsEmpty())
>      delete items()->takeFirst();
>
> But items() is apparently not accessible in this way is it?
>
> Or maybe what I am writing is not necessary for some reason that I
> failed to understand, can you please clarify the best way to instantiate
> new items, store them in the view, and delete them later on?
>
> Maybe I need to keep a list of the items I have stored (no problem with
> this, I will need it anyway), and then call putInCanvas(NULL) and delete
> on each one. Would this be OK? I though I could use the canvas->items()
> for this, but I am getting a C++ qualifier error accessing each item in
> this particular scenario.
>
> Regards,
> Mauricio
>


-- 
Ciao
Maurizio

"Well we all shine on
Like the moon and the stars and the sun" (John Lennon)


More information about the kde-games-devel mailing list