QGraphicsItemGroup and undo/redo error
Hello,
I've been using the Qt example project, undoframework, as a method for implementing undo/redo.
It has worked great for moves, rotations, resizes, creations, and deletions.
The approach uses lists of graphics items (QList<QGraphicsItems *>) for the redo and undo methods.
I've encountered an error with undo/redo with creating groups demonstrated by the following sequence:
1) create 2 items
2) create group from the 2 items
3) move group
4) undo 2 times (to where 2 items are just created)
5) redo (group operation)
6) redo (move operation) <<--- error, new group item doesn't match move's redo item
I create the group using QGraphicScene's createItemGroup() command. This creates a new pointer for the group each time it's called.
The move operation maintains a list of graphics items (the group item in this instance). Since, the group item pointer is different, an error occurs when re-doing the move operation.
Thanks,
Ben
Re: QGraphicsItemGroup and undo/redo error
Associate ID with your items, store all items in a dictionary indexed with the ID and in your undo/redo operations use the ID instead of pointers to items. Retrieve pointers from the dictionary based on ID when you need them.
Re: QGraphicsItemGroup and undo/redo error
Thanks for the tip!
I was wondering if I should do that on the way home last night ... stepping away helps to clear the head.
Re: QGraphicsItemGroup and undo/redo error
Quote:
Originally Posted by
brcain
Thanks for the tip!
I was wondering if I should do that on the way home last night ... stepping away helps to clear the head.
That's just one of the approaches. Another is not to delete items at all.
Re: QGraphicsItemGroup and undo/redo error
But this, do you mean to "disable" or remove the group items from the graphics scene?
I'm not sure how I'd go about doing that without having to manage the children myself. Currently, I'm relying on the QGraphicsScene to create the groups.
Although, this will likely break down when I add the rotate/resize controls to the groups like I have for the individual items. Then I'll likely have to create my own groups and manage their life cycle.
Re: QGraphicsItemGroup and undo/redo error
Basically for groups you can use QGraphicsItemGroup, regardless if it is the scene creating such instances or you. The main part is to avoid deleting items. Only an undo operation being removed from the stack wouldd perform an actual deletion. Grouping items is an easy operation regardless how you implement it. The most difficult operation is to do undo/redo for deleting items.
Re: QGraphicsItemGroup and undo/redo error
Perhaps I'm being obtuse ... but, I'm not following you.
I use QGraphicsScene's createItemGroup() and destroyItemGroup() in the redo and undo, respectively for grouping and ungrouping selected items.
The destroyItemGroup() method deletes the QGraphicsItemGroup ... unless you're suggesting I override it with my own and defer the deletion.
Re: QGraphicsItemGroup and undo/redo error
Let me see if I understand you ...
For a delete command,
- the redo hides the item[s] from the scene rather than deleting it
- the undo shows the item[s]
- the destructor (occurring when delete operation is removed from the stack) actually deletes the item[s]
Re: QGraphicsItemGroup and undo/redo error
Quote:
Originally Posted by
brcain
Let me see if I understand you ...
For a delete command,
- the redo hides the item[s] from the scene rather than deleting it
- the undo shows the item[s]
- the destructor (occurring when delete operation is removed from the stack) actually deletes the item[s]
Correct. Or use IDs everywhere.