QUndoCommand Signals Twice

  • I am using QUndoCommand to be able to have undo/redo facilities in my app. My QUndoCommand constructor adds an item to a list and then emits a signal that the addition was done. For some reason, the item ends up in the list twice. Now, I noticed that if I remove the adding and signal emitting from the my QUndoCommand constructor, the item gets added only once as it should be. The only other place where addition and emitting of the involved signal is in the redo function of the QUndoCommand. It seems that redo is called when the QUndoCommand is created. By removing the list addition and signal emitting from the redo function and keeping it in the constructor suggests this is indeed the case.

    Is it correct that QUndoCommand constructor calls redo at least once?

  • Well, it seems to be correct as when I remove the adding and signalling from the constructor everything works as expected. I presume this is what I should do?

  • Lifetime Qt Champion


    The idea is that the your custom command should handle everything between the undo and redo methods. When you add a QUndoCommand to the stack, its redo function will be called.

Log in to reply