|  | Home · Overviews · Examples

# QUndoStack Class Reference
[com.trolltech.qt.gui module]

The `QUndoStack` class is a stack of `QUndoCommand` objects. More...

Inherits `QObject`.

## Detailed Description

The `QUndoStack` class is a stack of `QUndoCommand` objects.

For an overview of Qt's Undo Framework, see the overview document.

An undo stack maintains a stack of commands that have been applied to a document.

New commands are pushed on the stack using `push`. Commands can be undone and redone using `undo` and `redo`, or by triggering the actions returned by `createUndoAction` and `createRedoAction`.

`QUndoStack` keeps track of the current command. This is the command which will be executed by the next call to `redo`. The index of this command is returned by `index`. The state of the edited object can be rolled forward or back using `setIndex`. If the top-most command on the stack has already been redone, `index` is equal to `count`.

`QUndoStack` provides support for undo and redo actions, command compression, command macros, and supports the concept of a clean state.

### Undo and Redo Actions

`QUndoStack` provides convenient undo and redo `QAction` objects, which can be inserted into a menu or a toolbar. When commands are undone or redone, `QUndoStack` updates the text properties of these actions to reflect what change they will trigger. The actions are also disabled when no command is available for undo or redo. These actions are returned by QUndoStack::createUndoAction() and QUndoStack::createRedoAction().

### Command Compression and Macros

Command compression is useful when several commands can be compressed into a single command that can be undone and redone in a single operation. For example, when a user types a character in a text editor, a new command is created. This command inserts the character into the document at the cursor position. However, it is more convenient for the user to be able to undo or redo typing of whole words, sentences, or paragraphs. Command compression allows these single-character commands to be merged into a single command which inserts or deletes sections of text. For more information, see QUndoCommand::mergeWith() and `push`.

A command macro is a sequence of commands, all of which are undone and redone in one go. Command macros are created by giving a command a list of child commands. Undoing or redoing the parent command will cause the child commands to be undone or redone. Command macros may be created explicitly by specifying a parent in the `QUndoCommand` constructor, or by using the convenience functions `beginMacro` and `endMacro`.

Although command compression and macros appear to have the same effect to the user, they often have different uses in an application. Commands that perform small changes to a document may be usefully compressed if there is no need to individually record them, and if only larger changes are relevant to the user. However, for commands that need to be recorded individually, or those that cannot be compressed, it is useful to use macros to provide a more convenient user experience while maintaining a record of each command.

### Clean State

`QUndoStack` supports the concept of a clean state. When the document is saved to disk, the stack can be marked as clean using `setClean`. Whenever the stack returns to this state through undoing and redoing commands, it emits the signal `cleanChanged`. This signal is also emitted when the stack leaves the clean state. This signal is usually used to enable and disable the save actions in the application, and to update the document's title to reflect that it contains unsaved changes.

See also `QUndoCommand` and `QUndoView`.