How can I delete a QCanvasItem in PyQt?

Baki Kose baba at localhost.localdomain
Sun Aug 18 05:40:28 CEST 2002


Baki Kose wrote:

> Hi All,
> 
> The following code is taken from canvas/canvas.cpp in the Qt examples
> directory. I am trying to implement this in PyQt, however since
> PyQt doesn't implement QCanvasItemList I am using Python list to
> hold the current QCanvasItem(s).
> 
> void FigureEditor::clear()
> {
>     QCanvasItemList list = canvas()->allItems();
>     QCanvasItemList::Iterator it = list.begin();
>     for (; it != list.end(); ++it) {
>         if ( *it )
>             delete *it;
>     }
> }
> 
> Here is how I (attempted) to re-implement this function
> in PyQt, but it doesn't delete the items. Can anyone
> tell me how I can fix this?
> 
>     def clear(self):
>         ilist = self.canvas.allItems()
>         for each_item in ilist:
>             if each_item:
>                 del each_item
> 
> Thanks
> sadi
I figured out a way (perheps not a good one, but ..) of doing this. Here is 
the working version of the above code:

    def clear(self):
        ilist = self.canvas().allItems()
        for each_item in ilist:
                each_item.setCanvas(None)
                del each_item
        self.canvas().update()

any ideas?
sadi



More information about the Python-list mailing list