[Qt-interest] deleteLater() for QGraphicsItem

Andreas Unger andi.unger05 at gmail.com
Thu Dec 3 07:28:21 CET 2009


>
> 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?
>

I'd like to clarify that what I'd like to do is some kind of type
introspection, meaning that I store a hash map of the QObject* and and int
(or String) and when I want to remove the items from the scene and call
deleteLater() on them, I iterate through the hash map, and for every key in
the hash map, I get a value which tells me what derived class the object
belongs to and I do a reinterpret_cast from a QObject* to MyItemClass*. Then
I remove the item from the scene and call deleteLater() on it. The easiest
way I can think of doing this is a bunch of ifs or a switch statement that
checks the string (or int) to see what derived class the QObject belongs to,
but I'm wondering whether there's a simpler Qt way of doing what I'm trying
to do or some elegant method/design pattern...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091203/806a578a/attachment.html 


More information about the Qt-interest-old mailing list