[PyKDE] QGraphisScene removeItem problem

V. Armando Sole sole at esrf.fr
Mon Dec 11 14:44:26 GMT 2006

At 14:20 11/12/2006 +0100, Remy Anthoine wrote:
>I'm having trouble using the removeItem operation of a scene in PyQt4
>with python 2.5 under windows XP. Can someone explain why the
>following produces an error of type : "python.exe has encountered a
>problem and needs to close. We are sorry for the inconvenience."
>import sys
>from PyQt4 import QtCore, QtGui
>class testtotrash(QtGui.QWidget):
>    def __init__(self,parent= None):
>        QtGui.QWidget.__init__(self, parent)
>        scene = QtGui.QGraphicsScene()
>        scene.addText("TEST")
>        for i in scene.items():
>            scene.removeItem(i)
>if __name__ == "__main__":
>    app = QtGui.QApplication(sys.argv)
>    myTTT = testtotrash()
>    myTTT.show()
>    sys.exit(app.exec_())
>I discovered this was a problem in a larger application where I
>sometimes remove all the items of a scene. It worked fine with
>ellipses or pixmaps but not since I started using QGraphicsTextItem.
>In the larger application, I get the following message along with
>windows' "python.exe has encountered..." :
>QGraphicsScene::removeItem: item 017098D8's scene (00000000) is
>different from this scene (016AF2E8)

I guess it is because when you delete an item, its children items are 
deleted too.

I delete all items from the scene (keeping a particular one) as:

     def __clearAll(self):
         Clear the print preview window, remove all items
         but and keep the page.
         itemlist = self.scene.items()
         keep = self.page
         while (len(itemlist) != 1):
             if itemlist.index(keep) == 0:
             itemlist = self.scene.items()

I hope that helps.


More information about the PyQt mailing list