QUndoCommand露
The
QUndoCommandclass is the base class of all commands stored on aQUndoStack. More鈥

Synopsis露
Functions露
def
actionText()def
child(index)def
childCount()def
isObsolete()def
setObsolete(obsolete)def
setText(text)def
text()
Virtual functions露
Detailed Description露
For an overview of Qt鈥檚 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 withredo()and undo the change withundo(). The implementations for these functions must be provided in a derived class.class AppendText(QUndoCommand): self.m_document = '' self.m_text = '' def AppendText(self, doc, text): self.m_document = doc self.m_text = text self.setText("append text") def undo(self): self.m_document.chop(self.m_text.length()) def redo(self): self.m_document->append(self.m_text)A
QUndoCommandhas an associatedtext(). This is a short string describing what the command does. It is used to update the text properties of the stack鈥檚 undo and redo actions; seecreateUndoAction()andcreateRedoAction().
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 deletedIn effect, when a command is pushed, it becomes the top-most command on the stack.
To support command compression,
QUndoCommandhas anid()and the virtual functionmergeWith(). These functions are used bypush().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鈥檛 provide its own implementation of
undo()andredo(). Instead, it uses the base implementations of these functions, which simply callundo()orredo()on all its children. The parent should, however, have a meaningfultext().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()andendMacro().See also
- class PySide2.QtWidgets.QUndoCommand([parent=None])露
PySide2.QtWidgets.QUndoCommand(text[, parent=None])
- param parent:
- param text:
str
Constructs a
QUndoCommandobject with parentparent.If
parentis notNone, this command is appended to parent鈥檚 child list. The parent command then owns this command and will delete it in its destructor.See also
~QUndoCommand()Constructs a
QUndoCommandobject with the givenparentandtext.If
parentis notNone, this command is appended to parent鈥檚 child list. The parent command then owns this command and will delete it in its destructor.See also
~QUndoCommand()
- PySide2.QtWidgets.QUndoCommand.actionText()露
- Return type:
str
Returns a short text string describing what this command does; for example, 鈥渋nsert text鈥.
The text is used when the text properties of the stack鈥檚 undo and redo actions are updated.
- PySide2.QtWidgets.QUndoCommand.child(index)露
- Parameters:
index 鈥 int
- Return type:
Returns the child command at
index.See also
- PySide2.QtWidgets.QUndoCommand.childCount()露
- Return type:
int
Returns the number of child commands in this command.
See also
- PySide2.QtWidgets.QUndoCommand.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鈥檚 class, or -1 if the command doesn鈥檛 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
- PySide2.QtWidgets.QUndoCommand.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 function is checked in the functions
push(),undo(),redo(), andsetIndex().See also
- PySide2.QtWidgets.QUndoCommand.mergeWith(other)露
- Parameters:
other 鈥
PySide2.QtWidgets.QUndoCommand- Return type:
bool
Attempts to merge this command with
command. Returnstrueon success; otherwise returnsfalse.If this function returns
true, calling this command鈥檚redo()must have the same effect as redoing both this command andcommand. Similarly, calling this command鈥檚undo()must have the same effect as undoingcommandand 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.class AppendText(QUndoCommand): ... def mergeWith(self, other): if other.id() != self.id(): # make sure other is also an AppendText command return False m_text += other.m_text return True
- PySide2.QtWidgets.QUndoCommand.redo()露
Applies a change to the document. This function must be implemented in the derived class. Calling
push(),undo()orredo()from this function leads to undefined beahavior.The default implementation calls on all child commands.
See also
- PySide2.QtWidgets.QUndoCommand.setObsolete(obsolete)露
- Parameters:
obsolete 鈥 bool
Sets whether the command is obsolete to
obsolete.See also
- PySide2.QtWidgets.QUndoCommand.setText(text)露
- Parameters:
text 鈥 str
Sets the command鈥檚 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()andactionText(), 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 functionactionText()are available since Qt 4.8.
- PySide2.QtWidgets.QUndoCommand.text()露
- Return type:
str
Returns a short text string describing what this command does; for example, 鈥渋nsert text鈥.
The text is used for names of items in
QUndoView.
- PySide2.QtWidgets.QUndoCommand.undo()露
Reverts a change to the document. After 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. Callingpush(),undo()orredo()from this function leads to undefined beahavior.The default implementation calls on all child commands in reverse order.
See also
漏 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.