How to remove last step from QUndoStack

  • Hi,
    I need to remove pushed last command from QUndoStack.

    How can i do this, thank you for your help.

  • Lifetime Qt Champion

    Hi and welcome
    Im not sure you can.
    For the push command
    it says
    "Once a command is pushed, the stack takes ownership of it. There are no getters to return the command, since modifying it after it has been executed will almost always lead to corruption of the document's state."

    So it dont sound like you cannot. I could be wrong. Did not work intensively with it.

  • Hi,
    the usual way to remove an command from the undo stack is to call the undo() function. Any consecutive new entry to the stack will eventually overwrite it then.

    But depending on what you want to do and why, there may be other options.

  • Hi,
    Yes calling undo() solves my problem. But after that i don't want to user can make redo(). So i need remove this action from QUndoStack.

  • Hi,

    yeah that's when you really want to abort an action. I do it the following way:

    • call undo()
    • push an empty command to the undo stack (one that re-done does in fact nothing and has an empty string title)
    • call undo() again

    It's a mean trick, but it works :-)

  • Hi Michael,
    Yes it seems this will solve my problem. As you say it'a a mean trick but that's ok. I will try, thank you very much.

Log in to reply