QUndoStack¶
The
QUndoStack
class is a stack ofQUndoCommand
objects. More…
Synopsis¶
Functions¶
def
beginMacro
(text)def
canRedo
()def
canUndo
()def
cleanIndex
()def
clear
()def
command
(index)def
count
()def
createRedoAction
(parent[, prefix=””])def
createUndoAction
(parent[, prefix=””])def
endMacro
()def
index
()def
isActive
()def
isClean
()def
push
(cmd)def
redoText
()def
setUndoLimit
(limit)def
text
(idx)def
undoLimit
()def
undoText
()
Slots¶
Signals¶
def
canRedoChanged
(canRedo)def
canUndoChanged
(canUndo)def
cleanChanged
(clean)def
indexChanged
(idx)def
redoTextChanged
(redoText)def
undoTextChanged
(undoText)
Detailed Description¶
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 usingundo()
andredo()
, or by triggering the actions returned bycreateUndoAction()
andcreateRedoAction()
.
QUndoStack
keeps track of thecurrent
command. This is the command which will be executed by the next call toredo()
. The index of this command is returned byindex()
. The state of the edited object can be rolled forward or back usingsetIndex()
. If the top-most command on the stack has already been redone,index()
is equal tocount()
.
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 redoQAction
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 bycreateUndoAction()
andcreateRedoAction()
.
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
mergeWith()
andpush()
.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 functionsbeginMacro()
andendMacro()
.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 usingsetClean()
. Whenever the stack returns to this state through undoing and redoing commands, it emits the signalcleanChanged()
. 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.
Obsolete Commands¶
QUndoStack
is able to delete commands from the stack if the command is no longer needed. One example may be to delete a command when two commands are merged together in such a way that the merged command has no function. This can be seen with move commands where the user moves their mouse to one part of the screen and then moves it to the original position. The merged command results in a mouse movement of 0. This command can be deleted since it serves no purpose. Another example is with networking commands that fail due to connection issues. In this case, the command is to be removed from the stack because theredo()
andundo()
functions have no function since there was connection issues.A command can be marked obsolete with the
setObsolete()
function. TheisObsolete()
flag is checked inpush()
,undo()
,redo()
, andsetIndex()
after callingundo()
,redo()
andQUndoCommand
:mergeWith() where applicable.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.
See also
- class PySide2.QtWidgets.QUndoStack([parent=None])¶
- param parent:
Constructs an empty undo stack with the parent
parent
. The stack will initially be in the clean state. Ifparent
is aQUndoGroup
object, the stack is automatically added to the group.See also
- PySide2.QtWidgets.QUndoStack.beginMacro(text)¶
- Parameters:
text – str
Begins composition of a macro command with the given
text
description.An empty command described by the specified
text
is pushed on the stack. Any subsequent commands pushed on the stack will be appended to the empty command’s children untilendMacro()
is called.Calls to and
endMacro()
may be nested, but every call to must have a matching call toendMacro()
.While a macro is being composed, the stack is disabled. This means that:
indexChanged()
andcleanChanged()
are not emitted,the undo/redo actions are disabled.
The stack becomes enabled and appropriate signals are emitted when
endMacro()
is called for the outermost macro.stack.beginMacro("insert red text") stack.push(InsertText(document, idx, text)) stack.push(SetColor(document, idx, text.length(), Qt.red)) stack.endMacro() # indexChanged() is emitted
This code is equivalent to:
insertRed = QUndoCommand() # an empty command insertRed.setText("insert red text") InsertText(document, idx, text, insertRed) # becomes child of insertRed SetColor(document, idx, text.length(), Qt.red, insertRed) stack.push(insertRed)
See also
- PySide2.QtWidgets.QUndoStack.canRedo()¶
- Return type:
bool
This property holds whether this stack can redo..
This property indicates whether or not there is a command that can be redone.
- PySide2.QtWidgets.QUndoStack.canRedoChanged(canRedo)¶
- Parameters:
canRedo – bool
- PySide2.QtWidgets.QUndoStack.canUndo()¶
- Return type:
bool
This property holds whether this stack can undo..
This property indicates whether or not there is a command that can be undone.
- PySide2.QtWidgets.QUndoStack.canUndoChanged(canUndo)¶
- Parameters:
canUndo – bool
- PySide2.QtWidgets.QUndoStack.cleanChanged(clean)¶
- Parameters:
clean – bool
- PySide2.QtWidgets.QUndoStack.cleanIndex()¶
- Return type:
int
Returns the clean index. This is the index at which
setClean()
was called.A stack may not have a clean index. This happens if a document is saved, some commands are undone, then a new command is pushed. Since
push()
deletes all the undone commands before pushing the new command, the stack can’t return to the clean state again. In this case, this function returns -1. The -1 may also be returned after an explicit call toresetClean()
.See also
- PySide2.QtWidgets.QUndoStack.clear()¶
Clears the command stack by deleting all commands on it, and returns the stack to the clean state.
Commands are not undone or redone; the state of the edited object remains unchanged.
This function is usually used when the contents of the document are abandoned.
See also
QUndoStack()
- PySide2.QtWidgets.QUndoStack.command(index)¶
- Parameters:
index – int
- Return type:
Returns a const pointer to the command at
index
.This function returns a const pointer, because modifying a command, once it has been pushed onto the stack and executed, almost always causes corruption of the state of the document, if the command is later undone or redone.
See also
- PySide2.QtWidgets.QUndoStack.count()¶
- Return type:
int
Returns the number of commands on the stack. Macro commands are counted as one command.
See also
- PySide2.QtWidgets.QUndoStack.createRedoAction(parent[, prefix=""])¶
- Parameters:
parent –
PySide2.QtCore.QObject
prefix – str
- Return type:
Creates an redo
QAction
object with the givenparent
.Triggering this action will cause a call to
redo()
. The text of this action is the text of the command which will be redone in the next call toredo()
, prefixed by the specifiedprefix
. If there is no command available for redo, this action will be disabled.If
prefix
is empty, the default template “Redo %1” is used instead of prefix. Before Qt 4.8, the prefix “Redo” was used by default.See also
- PySide2.QtWidgets.QUndoStack.createUndoAction(parent[, prefix=""])¶
- Parameters:
parent –
PySide2.QtCore.QObject
prefix – str
- Return type:
Creates an undo
QAction
object with the givenparent
.Triggering this action will cause a call to
undo()
. The text of this action is the text of the command which will be undone in the next call toundo()
, prefixed by the specifiedprefix
. If there is no command available for undo, this action will be disabled.If
prefix
is empty, the default template “Undo %1” is used instead of prefix. Before Qt 4.8, the prefix “Undo” was used by default.See also
- PySide2.QtWidgets.QUndoStack.endMacro()¶
Ends composition of a macro command.
If this is the outermost macro in a set nested macros, this function emits
indexChanged()
once for the entire macro command.See also
- PySide2.QtWidgets.QUndoStack.index()¶
- Return type:
int
Returns the index of the current command. This is the command that will be executed on the next call to
redo()
. It is not always the top-most command on the stack, since a number of commands may have been undone.See also
- PySide2.QtWidgets.QUndoStack.indexChanged(idx)¶
- Parameters:
idx – int
- PySide2.QtWidgets.QUndoStack.isActive()¶
- Return type:
bool
This property holds the active status of this stack..
An application often has multiple undo stacks, one for each opened document. The active stack is the one associated with the currently active document. If the stack belongs to a
QUndoGroup
, calls toundo()
orredo()
will be forwarded to this stack when it is active. If theQUndoGroup
is watched by aQUndoView
, the view will display the contents of this stack when it is active. If the stack does not belong to aQUndoGroup
, making it active has no effect.It is the programmer’s responsibility to specify which stack is active by calling , usually when the associated document window receives focus.
See also
- PySide2.QtWidgets.QUndoStack.isClean()¶
- Return type:
bool
This property holds the clean status of this stack..
This property indicates whether or not the stack is clean. For example, a stack is clean when a document has been saved.
See also
- PySide2.QtWidgets.QUndoStack.push(cmd)¶
- Parameters:
Pushes
cmd
on the stack or merges it with the most recently executed command. In either case, executescmd
by calling itsredo()
function.If
cmd
‘s id is not -1, and if the id is the same as that of the most recently executed command,QUndoStack
will attempt to merge the two commands by callingmergeWith()
on the most recently executed command. IfmergeWith()
returnstrue
,cmd
is deleted.After calling
redo()
and, if applicable,mergeWith()
,isObsolete()
will be called forcmd
or the merged command. IfisObsolete()
returnstrue
, thencmd
or the merged command will be deleted from the stack.In all other cases
cmd
is simply pushed on the stack.If commands were undone before
cmd
was pushed, the current command and all commands above it are deleted. Hencecmd
always ends up being the top-most on the stack.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.
See also
- PySide2.QtWidgets.QUndoStack.redo()¶
Redoes the current command by calling
redo()
. Increments the current command index.If the stack is empty, or if the top command on the stack has already been redone, this function does nothing.
If
isObsolete()
returns true for the current command, then the command will be deleted from the stack. Additionally, if the clean index is greater than or equal to the current command index, then the clean index is reset.
- PySide2.QtWidgets.QUndoStack.redoText()¶
- Return type:
str
This property holds the redo text of the next command that is redone..
This property holds the text of the command which will be redone in the next call to
redo()
.See also
- PySide2.QtWidgets.QUndoStack.redoTextChanged(redoText)¶
- Parameters:
redoText – str
- PySide2.QtWidgets.QUndoStack.resetClean()¶
Leaves the clean state and emits
cleanChanged()
if the stack was clean. This method resets the clean index to -1.This is typically called in the following cases, when a document has been:
created basing on some template and has not been saved, so no filename has been associated with the document yet.
restored from a backup file.
changed outside of the editor and the user did not reload it.
See also
- PySide2.QtWidgets.QUndoStack.setActive([active=true])¶
- Parameters:
active – bool
This property holds the active status of this stack..
An application often has multiple undo stacks, one for each opened document. The active stack is the one associated with the currently active document. If the stack belongs to a
QUndoGroup
, calls toundo()
orredo()
will be forwarded to this stack when it is active. If theQUndoGroup
is watched by aQUndoView
, the view will display the contents of this stack when it is active. If the stack does not belong to aQUndoGroup
, making it active has no effect.It is the programmer’s responsibility to specify which stack is active by calling , usually when the associated document window receives focus.
See also
- PySide2.QtWidgets.QUndoStack.setClean()¶
Marks the stack as clean and emits
cleanChanged()
if the stack was not already clean.This is typically called when a document is saved, for example.
Whenever the stack returns to this state through the use of undo/redo commands, it emits the signal
cleanChanged()
. This signal is also emitted when the stack leaves the clean state.See also
- PySide2.QtWidgets.QUndoStack.setIndex(idx)¶
- Parameters:
idx – int
Repeatedly calls
undo()
orredo()
until the current command index reachesidx
. This function can be used to roll the state of the document forwards of backwards.indexChanged()
is emitted only once.
- PySide2.QtWidgets.QUndoStack.setUndoLimit(limit)¶
- Parameters:
limit – int
This property holds the maximum number of commands on this stack..
When the number of commands on a stack exceedes the stack’s , commands are deleted from the bottom of the stack. Macro commands (commands with child commands) are treated as one command. The default value is 0, which means that there is no limit.
This property may only be set when the undo stack is empty, since setting it on a non-empty stack might delete the command at the current index. Calling on a non-empty stack prints a warning and does nothing.
- PySide2.QtWidgets.QUndoStack.text(idx)¶
- Parameters:
idx – int
- Return type:
str
Returns the text of the command at index
idx
.See also
- PySide2.QtWidgets.QUndoStack.undo()¶
Undoes the command below the current command by calling
undo()
. Decrements the current command index.If the stack is empty, or if the bottom command on the stack has already been undone, this function does nothing.
After the command is undone, if
isObsolete()
returnstrue
, then the command will be deleted from the stack. Additionally, if the clean index is greater than or equal to the current command index, then the clean index is reset.
- PySide2.QtWidgets.QUndoStack.undoLimit()¶
- Return type:
int
This property holds the maximum number of commands on this stack..
When the number of commands on a stack exceedes the stack’s , commands are deleted from the bottom of the stack. Macro commands (commands with child commands) are treated as one command. The default value is 0, which means that there is no limit.
This property may only be set when the undo stack is empty, since setting it on a non-empty stack might delete the command at the current index. Calling on a non-empty stack prints a warning and does nothing.
- PySide2.QtWidgets.QUndoStack.undoText()¶
- Return type:
str
This property holds the undo text of the next command that is undone..
This property holds the text of the command which will be undone in the next call to
undo()
.See also
- PySide2.QtWidgets.QUndoStack.undoTextChanged(undoText)¶
- Parameters:
undoText – str
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.