Important: Please read the Qt Code of Conduct -

Undo by saving the whole model-state not actions possible with QUndoStack?

  • Hi,

    i have a project where there is a QAbstractItemModel that can be manipulated by some actions.
    The changes to the model sometimes are a bit complicated, so it is hard to keep track of all the action and the opposite of them.
    Therefore I think the easiest way to implement Undo / Redo would be to just save the model state to a stack each time something is changed and load the old model for undo.

    The problem I see with this approach is, that for loading back the old model I also have to "undo" the beginInsertColumns() beginRemoveRows() begindeleteRows() and so on for the ItemView.

    Is there maybe some example for such a case?

  • Even if it might be quite inefficient, if you have control over the model, you can call beginResetModel/endResetModel to signal the complete change to the view

  • @VRonin: Thanks for the hint.
    I got that approach working now, however every time I undo the whole treeView collapses.
    Is there some Method to prevent this?

Log in to reply