Results 1 to 9 of 9

Thread: QGraphicsItemGroup and undo/redo error

  1. #1
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default 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
    Last edited by brcain; 17th December 2013 at 00:44.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    brcain (17th December 2013)

  4. #3
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default 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.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsItemGroup and undo/redo error

    Quote Originally Posted by brcain View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default 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.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default 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.

  9. #8
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default 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]

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsItemGroup and undo/redo error

    Quote Originally Posted by brcain View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. qt undo/redo
    By giugio in forum Qt Programming
    Replies: 1
    Last Post: 12th November 2012, 16:31
  2. undo/redo example has refactored
    By n_vova in forum Qt Programming
    Replies: 1
    Last Post: 10th May 2012, 10:04
  3. Undo/Redo issues of QTextEdit
    By Maximka in forum Newbie
    Replies: 0
    Last Post: 22nd February 2010, 07:36
  4. QTextEdit undo/redo
    By Derf in forum Qt Programming
    Replies: 1
    Last Post: 6th August 2009, 09:12
  5. Implement Undo Redo
    By ankurjain in forum Qt Programming
    Replies: 5
    Last Post: 28th March 2006, 13:17

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.