Help - Search - Members - Calendar
Full Version: Deleting Qcanvasitem
OESF Forums > Distros, Development, and Model Specific Forums > Everything Development > Python
siggy
Hi Guys

I've got a GO game board drawing to a QCanvasView using QCanvasPolyItems.
When I want to redraw the board (on resize event, etc) I think I'll need to delete all the items on the canvas & redraw them to the new sizes. [I guess I could scale them, but anyway...].

So my question is how do you delete a QCanvasItem from a QCanvas?

I know I can get a list of items on the canvas with the allItems() member func, but I can seem to find a delete or clear type func for the QCanvasItem.

Cheers

Siggy
kopsis
QUOTE(siggy @ Jan 5 2005, 05:29 AM)
When I want to redraw the board (on resize event, etc) I think I'll need to delete all the items on the canvas & redraw them to the new sizes. [I guess I could scale them, but anyway...].

I think "rescaling" the QCanvasItems would be the normal way to deal with that. Barring that, you might as well just delete the whole QCanvas. If there are no other references to your QCanvasItems, then they should automatically get deleted from memory when their QCanvas goes away.
siggy
Hi Kopsis

thanks for the reply.

I found the del() method, so......

CODE
items = self.canvas.allItems()
       for item in items:
           item.hide()
           del(item)    # this is what I needed


....does the trick.

As you suggest, I will look at the scale option as well, as this must be more efficient than deleting & recreating canvas items...... anyway I'll save that for later.

As I'm wanting to keep the GO board (with a constant aspect ratio) in the centre of the CanvasView (which may have a changed aspect ratio), I'll also need to recalc the placement of the Canvas items.

Obviously as I'm just starting out with both Python & PyQt I'll make / am making loads of mistakes smile.gif But hopefully attempting to create this small GO / sgf viewer app is giving me a good learning platform.

Thanks again for the reply, it's much appreciated

Siggy
siggy
Okay, so the code I showed dosn't seem to work!?!?!?

When I delete items the count of items on the canvas keeps going up.
Ie, first time I delete, 48 items are found on the canvas & 'deleted', next time 96 items found on the canvas, etc. So the del(item) doesn't seem to be deleting the item from the canvas!

This seems ridiculous!
You can add Items to a canvas, you can then hide or show them, but you cann't pysically delete or remove them from a canvas!?! Surely not!

Oh, well...
So only way I could get it to work as I'd anticipated it (ie, delete 48 items was to delete & create the QCanvas - as you suggested smile.gif )

Still, I can't believe I cann't manage to delete an Item(s) from off the Canvas!?!?

Siggy
slapout
You're way ahead of me. I've never used a QCanvas (at least not yet :-). But I just looked at the qt docs and it seems that QCanvas has a removeItem method. Have you tried that?

http://doc.trolltech.com/qtopia1.6/html/qcanvas.html

Nevermind. My bad. It says "For internal use only. "
siggy
Hi All

I finally found the answer to this by entering 'delete QCanvasItem' in google & then going to the FAQ at 'http://www.riverbankcomputing.co.uk/pyqt/faq.php'

Who'd of guessed it would be that simple smile.gif

here's the answer for those interested:-

How do I delete a QCanvasItem?

QCanvasItem instances are owned by their parent QCanvas so that even if you delete the Python object, the underlying C++ instance still exists. The solution is to use the QCanvasItem.setCanvas() method and pass None as the parameter. If you then delete the Python object, the underlying C++ instance will also be destroyed.

Siggy
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2014 Invision Power Services, Inc.