Author Topic: Deleting Qcanvasitem  (Read 4721 times)

siggy

  • Newbie
  • *
  • Posts: 20
    • View Profile
Deleting Qcanvasitem
« on: January 05, 2005, 05:29:00 am »
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

  • Sr. Member
  • ****
  • Posts: 329
    • View Profile
    • http://kopsisengineering.com
Deleting Qcanvasitem
« Reply #1 on: January 05, 2005, 03:55:59 pm »
Quote
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

  • Newbie
  • *
  • Posts: 20
    • View Profile
Deleting Qcanvasitem
« Reply #2 on: January 06, 2005, 05:21:56 am »
Hi Kopsis

thanks for the reply.

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

Code: [Select]
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   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

  • Newbie
  • *
  • Posts: 20
    • View Profile
Deleting Qcanvasitem
« Reply #3 on: January 06, 2005, 06:11:18 am »
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  )

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

Siggy
« Last Edit: January 06, 2005, 06:47:41 am by siggy »

slapout

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Deleting Qcanvasitem
« Reply #4 on: January 06, 2005, 11:43:15 am »
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. "
« Last Edit: January 06, 2005, 03:29:13 pm by slapout »
SL-5600
Watapon 1.2 Rom

siggy

  • Newbie
  • *
  • Posts: 20
    • View Profile
Deleting Qcanvasitem
« Reply #5 on: January 12, 2005, 07:41:40 am »
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  

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