PDA

View Full Version : QGraphicsItemGroup and undo/redo error



brcain
16th December 2013, 22:05
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

wysota
17th December 2013, 08:46
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.

brcain
17th December 2013, 14:46
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.

wysota
17th December 2013, 15:55
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.

brcain
17th December 2013, 16:01
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.

wysota
17th December 2013, 17:03
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.

brcain
17th December 2013, 17:14
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.

brcain
18th December 2013, 02:23
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]

wysota
18th December 2013, 08:06
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.