[Qt-interest] deleteLater() for QGraphicsItem
Andreas Unger
andi.unger05 at gmail.com
Wed Dec 2 20:45:37 CET 2009
Hello all,
I have a QGraphicsView with a bunch of objects that inherit from
QGraphicsItem (QGraphicsRectItem or QGraphicsPixmapItem) and QObject, and
QGraphicsProxyWidgets. For some reason, I can't delete the items directly
without the application crashing -- I have to use deleteLater()
Since QGraphicsScene::clear() deletes all items which causes my application
to crash, I'm trying to maintain a list of the items that have been added to
the scene that will be removed and deleted later on. Since the items inherit
from QObject, I can directly call deleteLater() on them. However, if I add
them the items to a list, I have to cast them to a common type that they all
inherit from such as QGraphicsItem or QObject.
Given that QGraphicsItem has no deleteLater() method, I cast these items to
QObject* and in my customClear() method, I remove all the items in my list
of items from the scene and call deleteLater() on them, then I call
QGraphicsScene::clear(). If I dynamically cast every QObject* in the list to
QGraphicsItem, I can remove the item from the scene, call deleteLater() on
it. Then I can call QGraphicsScene::clear() since all items that could not
be safely deleted using 'delete' as opposed to 'deleteLater()' have already
been removed from the scene. That works fine, I think.
I think that dynamic casts cause a runtime overhead that I want to avoid. If
I do a reinterpret_cast from QObject* to QGraphicsItem* (so that I can be
able to call the QGraphicsScene::removeItem method), the address of the
QGraphicsItem* is no longer the same and I can thus not remove the item, and
an eventual QGraphicsSceneClear() causes a crash, since the original pointer
hasn't been removed from the scene()->items() list.
I hope I've explained myself well. Can anyone suggest to me a better way of
doing what I'm trying to do?
Thanks,
Andreas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091202/b0fe5eb5/attachment.html
More information about the Qt-interest-old
mailing list