user2567875
user2567875

Reputation: 502

QUndostack remove item above current index

I have a QUndoStack that looks like

2 - something (active)
1 - something
0 - bottom of stack

Two situations are possible now: I: open a dialog which pushes several things to the stack merged as one and click "accept", the stack looks like this:

3 - dialog done (active)
2 - something
1 - something
0 - bottom of stack

II: open a dialog which pushes several things to the stack merged as one and click "cancel", the stack looks like this:

3 - dialog done
2 - something (active)
1 - something
0 - bottom of stack

(I) is ok, (II) is technicaly also ok, but I want (3) be gone:

2 - something (active)
1 - something
0 - bottom of stack

As soon the user does something else the stack looks fine again:

3 - new thing done (active)
2 - something 
1 - something
0 - bottom of stack

But I want to remove the command entirely from the stack if the user pushed cancel, the command is never used again and only confuses the user if present. The stack should look like before, as nothing happend.

I found

void QUndoCommand::setObsolete(bool obsolete)

which i assume is the right command to tell the stack to remove the item, documentation says:

  If a command is set obsolete 
  and the clean index is greater than or equal to the current command index, 
  then the clean index will be reset 
  when the command is deleted from the stack.

but I wonder how to access the merged commands? I am afraid

const QUndoCommand *QUndoCommand::child(int index) const
const QUndoCommand *QUndoStack::command(int index) const

are not the right ways to go, since they are given back as const and because of the documentation warnings.

https://doc.qt.io/qt-5/qundostack.html https://doc.qt.io/qt-5/qundocommand.html#setObsolete

Upvotes: 0

Views: 387

Answers (1)

Martin Hennings
Martin Hennings

Reputation: 16846

If I understand you correctly, you are using the topmost command as a 'preview' to show what accepting the open dialog would do.

In this case, and if I also understand Qt's documentation correctly, you should setObsolete(true) the topmost ('preview') command and then undo it. That should delete it from the stack.

The merged command is (as I interpret your question) the complete effect that accepting the dialog would have, so this entire command is obsolete.

Upvotes: 2

Related Questions