I was using the QT undo framework example as reference to implement this functionality in my tool. However, it seems to have a bug with the way that it calls the destructor of the items.
I understand that QGraphicsScene will assume ownership of the items while they are in the scene. However, both undo objects: AddCommand and RemoveCommand should assume ownership of these items when they remove them from the scene.
In the Qt undo framework example, only AddCommand tries to delete the object in its destructor, but will not do it if the item is still in the scene.
AddCommand::~AddCommand()
{
if (!myDiagramItem->scene())
delete myDiagramItem;
}
In this case, if we remove the item from the scene after the corresponding AddCommand object leave the stack (when using an undo limit), the item will never be deleted again since RemoveCommand destructor doesn't do it.