PySide6.QtGui.QUndoCommand¶
- class QUndoCommand¶
- The - QUndoCommandclass is the base class of all commands stored on a- QUndoStack. More…- Synopsis¶- Methods¶- def - __init__()
- def - actionText()
- def - child()
- def - childCount()
- def - isObsolete()
- def - setObsolete()
- def - setText()
- def - text()
 - Virtual methods¶- def - id()
- def - mergeWith()
- def - redo()
- def - undo()
 - Note - This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE - Detailed Description¶- Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - For an overview of Qt’s Undo Framework, see the overview document. - A - QUndoCommandrepresents a single editing action on a document; for example, inserting or deleting a block of text in a text editor.- QUndoCommandcan apply a change to the document with- redo()and undo the change with- undo(). The implementations for these functions must be provided in a derived class.- class AppendText(QUndoCommand): # public AppendText(QString doc, QString text) self.m_document = doc self.m_text = text) { setText("append text" def undo(): { m_document.chop(m_text.length()); } def redo(): { m_document.append(m_text); } # private m_document = QString() m_text = QString() - A - QUndoCommandhas an associated- text(). This is a short string describing what the command does. It is used to update the text properties of the stack’s undo and redo actions; see- createUndoAction()and- createRedoAction().- QUndoCommandobjects are owned by the stack they were pushed on.- QUndoStackdeletes a command if it has been undone and a new command is pushed. For example:- command1 = MyCommand() stack.push(command1) command2 = MyCommand() stack.push(command2) stack.undo() command3 = MyCommand() stack.push(command3) # command2 gets deleted - In effect, when a command is pushed, it becomes the top-most command on the stack. - To support command compression, - QUndoCommandhas an- id()and the virtual function- mergeWith(). These functions are used by- push().- To support command macros, a - QUndoCommandobject can have any number of child commands. Undoing or redoing the parent command will cause the child commands to be undone or redone. A command can be assigned to a parent explicitly in the constructor. In this case, the command will be owned by the parent.- The parent in this case is usually an empty command, in that it doesn’t provide its own implementation of - undo()and- redo(). Instead, it uses the base implementations of these functions, which simply call- undo()or- redo()on all its children. The parent should, however, have a meaningful- text().- 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) - Another way to create macros is to use the convenience functions - beginMacro()and- endMacro().- See also - __init__([parent=None])¶
- Parameters:
- parent – - QUndoCommand
 
 - Constructs a - QUndoCommandobject with parent- parent.- If - parentis not- None, this command is appended to parent’s child list. The parent command then owns this command and will delete it in its destructor.- See also - ~QUndoCommand()- __init__(text[, parent=None])
- Parameters:
- text – str 
- parent – - QUndoCommand
 
 
 - Constructs a - QUndoCommandobject with the given- parentand- text.- If - parentis not- None, this command is appended to parent’s child list. The parent command then owns this command and will delete it in its destructor.- See also - ~QUndoCommand()- actionText()¶
- Return type:
- str 
 
 - Returns a short text string describing what this command does; for example, “insert text”. - The text is used when the text properties of the stack’s undo and redo actions are updated. - child(index)¶
- Parameters:
- index – int 
- Return type:
 
 - Returns the child command at - index.- See also - childCount()¶
- Return type:
- int 
 
 - Returns the number of child commands in this command. - See also - id()¶
- Return type:
- int 
 
 - Returns the ID of this command. - A command ID is used in command compression. It must be an integer unique to this command’s class, or -1 if the command doesn’t support compression. - If the command supports compression this function must be overridden in the derived class to return the correct ID. The base implementation returns -1. - push()will only try to merge two commands if they have the same ID, and the ID is not -1.- See also - isObsolete()¶
- Return type:
- bool 
 
 - Returns whether the command is obsolete. - The boolean is used for the automatic removal of commands that are not necessary in the stack anymore. The isObsolete function is checked in the functions - push(),- undo(),- redo(), and- setIndex().- See also - mergeWith(other)¶
- Parameters:
- other – - QUndoCommand
- Return type:
- bool 
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Attempts to merge this command with - command. Returns- trueon success; otherwise returns- false.- If this function returns - true, calling this command’s- redo()must have the same effect as redoing both this command and- command. Similarly, calling this command’s- undo()must have the same effect as undoing- commandand this command.- QUndoStackwill only try to merge two commands if they have the same id, and the id is not -1.- The default implementation returns - false.- redo()¶
 - Applies a change to the document. This function must be implemented in the derived class. Calling - push(),- undo()or- redo()from this function leads to undefined beahavior.- The default implementation calls redo() on all child commands. - See also - setObsolete(obsolete)¶
- Parameters:
- obsolete – bool 
 
 - Sets whether the command is obsolete to - obsolete.- See also - setText(text)¶
- Parameters:
- text – str 
 
 - Sets the command’s text to be the - textspecified.- The specified text should be a short user-readable string describing what this command does. - If you need to have two different strings for - text()and- actionText(), separate them with “\n” and pass into this function. Even if you do not use this feature for English strings during development, you can still let translators use two different strings in order to match specific languages’ needs. The described feature and the function- actionText()are available since Qt 4.8.- text()¶
- Return type:
- str 
 
 - Returns a short text string describing what this command does; for example, “insert text”. - The text is used for names of items in QUndoView. - undo()¶
 - Reverts a change to the document. After undo() is called, the state of the document should be the same as before - redo()was called. This function must be implemented in the derived class. Calling- push(),- undo()or- redo()from this function leads to undefined beahavior.- The default implementation calls undo() on all child commands in reverse order. - See also