OESF | ELSI | pdaXrom | OpenZaurus | Zaurus Themes | Community Links | Ibiblio

IPB

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Deleting Qcanvasitem
siggy
post Jan 5 2005, 02:29 AM
Post #1





Group: Members
Posts: 20
Joined: 13-December 04
Member No.: 5,907



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
Go to the top of the page
 
+Quote Post
kopsis
post Jan 5 2005, 12:55 PM
Post #2





Group: Members
Posts: 329
Joined: 1-July 04
Member No.: 3,880



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.
Go to the top of the page
 
+Quote Post
siggy
post Jan 6 2005, 02:21 AM
Post #3





Group: Members
Posts: 20
Joined: 13-December 04
Member No.: 5,907



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
Go to the top of the page
 
+Quote Post
siggy
post Jan 6 2005, 03:11 AM
Post #4





Group: Members
Posts: 20
Joined: 13-December 04
Member No.: 5,907



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
Go to the top of the page
 
+Quote Post
slapout
post Jan 6 2005, 08:43 AM
Post #5





Group: Members
Posts: 50
Joined: 10-November 03
Member No.: 832



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. "
Go to the top of the page
 
+Quote Post
siggy
post Jan 12 2005, 04:41 AM
Post #6





Group: Members
Posts: 20
Joined: 13-December 04
Member No.: 5,907



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
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



RSS Lo-Fi Version Time is now: 28th July 2014 - 10:27 AM