Results 1 to 5 of 5

Thread: How to get a return value from QUndoCommand::redo?

  1. #1
    Join Date
    Jan 2012
    Posts
    66
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Platforms
    Windows

    Default How to get a return value from QUndoCommand::redo?

    I'm using a custom QGraphicsScene in which you can click to create a custom QGraphicsItem, then drag (with the mouse still held down) to resize it. To do this, overrode QGraphicsItem::mousePressEvent and mouseMoveEvent, and it worked fine. It was something like this:

    Qt Code:
    1. MyScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
    2. {
    3. itemBeingCreated = new MyItem(event->pos()) // "itemBeingCreated" is declared in myscene.h
    4. }
    5.  
    6. MyScene::mouseMoveEvent(QGraphicsMouseEvent *event)
    7. {
    8. itemBeingCreated->resize(/* etc */) // basically how far you drag it from its origin, that's how big it'll be
    9. }
    To copy to clipboard, switch view to plain text mode 

    Now I am trying to add undo/redo capability with QUndoStack and QUndoCommand. So now my mousePressEvent does this

    Qt Code:
    1. MyScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
    2. {
    3. undoStack->push(new AddNewItem(/* etc */)); // "AddNewItem" is a QUndoCommand
    4. }
    To copy to clipboard, switch view to plain text mode 

    But I don't understand how I can access the item created by "AddNewItem" in order to resize it in MyScene:nMouseMove. Should I just implement a "QGraphicsItem *AddNewItem::getCreatedItem()", or will that "break" the command pattern and lead to problems?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to get a return value from QUndoCommand::redo?

    Why did you discard the code you already had that kept track of the pointer to the new item?

    Personally though, I'd wait until the mouse release event to determine:
    • That the added item is big enough to be worth adding. This avoids accidental short click-drag-release creating hard to see/use items.
    • That the user has not cancelled the creation, e.g. by pressing escape, if you allow that during the create event.
    • And the final size of the added item is known.

  3. #3
    Join Date
    Jan 2012
    Posts
    66
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Platforms
    Windows

    Default Re: How to get a return value from QUndoCommand::redo?

    I should have clarified that AddNewItem both creates the item (and pointer) and adds it to the scene. Thus, the code that kept track of the pointer is now inside of AddNewItem::redo(). Of course, the pointer is useless there, and goes out of scope after AddNewItem::redo().

    Are you suggesting that I should create a null pointer to the item and pass it to AddNewItem's constructor, and have AddNewItem::redo() create the item and assign it to the previously null pointer?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to get a return value from QUndoCommand::redo?

    No, create the item and keep the pointer. Construct the AddNewItem object with the pointer, i.e. give AddNewItem's constructor and argument, and add it to the stack.

    Edit: Ignore that. I see where you are going.

    Consider moving the item creation to the end of the user's creation click-n-drag and then you don't have the issue.

  5. The following user says thank you to ChrisW67 for this useful post:

    wayfaerer (8th March 2012)

  6. #5
    Join Date
    Jan 2012
    Posts
    66
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Platforms
    Windows

    Default Re: How to get a return value from QUndoCommand::redo?

    Thanks, I understand now.

    I had a closer look at the Undo Framework example, and their AddCommand's redo and undo methods just added/removed the item from the scene, whereas in my case, redo and undo actually created and deleted the item (AddNewItem's constructor stored all of the necessary info to re-create the item after it was deleted). So I'll do what you suggested, and change the redo and undo functions.

Similar Threads

  1. Cancel to QUndoStack in redo()
    By iwatsu in forum Newbie
    Replies: 11
    Last Post: 1st April 2015, 09:45
  2. undo/redo example has refactored
    By n_vova in forum Qt Programming
    Replies: 1
    Last Post: 10th May 2012, 10:04
  3. Replies: 0
    Last Post: 3rd August 2010, 11:47
  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.