内容表

上一话题

QTreeWidgetItemIterator

下一话题

QUndoGroup

QUndoCommand

QUndoCommand class is the base class of all commands stored on a QUndoStack . 更多

Inheritance diagram of PySide2.QtWidgets.QUndoCommand

概要

函数

虚函数

详细描述

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

A QUndoCommand represents a single editing action on a document; for example, inserting or deleting a block of text in a text editor. QUndoCommand can 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):
    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 QUndoCommand has 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() .

QUndoCommand objects are owned by the stack they were pushed on. QUndoStack deletes 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, QUndoCommand has an id() and the virtual function mergeWith() . These functions are used by push() .

To support command macros, a QUndoCommand object 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() .

另请参阅

QUndoStack

class QUndoCommand ( [ parent=None ] )

QUndoCommand(text[, parent=None])

param parent

QUndoCommand

param text

unicode

构造 QUndoCommand object with parent parent .

parent 不是 None , this command is appended to parent’s child list. The parent command then owns this command and will delete it in its destructor.

另请参阅

~QUndoCommand()

构造 QUndoCommand 对象采用给定 parent and text .

parent 不是 None , this command is appended to parent’s child list. The parent command then owns this command and will delete it in its destructor.

另请参阅

~QUndoCommand()

PySide2.QtWidgets.QUndoCommand. actionText ( )
返回类型

unicode

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.

PySide2.QtWidgets.QUndoCommand. child ( index )
参数

index int

返回类型

QUndoCommand

Returns the child command at index .

PySide2.QtWidgets.QUndoCommand. childCount ( )
返回类型

int

Returns the number of child commands in this command.

另请参阅

child()

PySide2.QtWidgets.QUndoCommand. id ( )
返回类型

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.

PySide2.QtWidgets.QUndoCommand. isObsolete ( )
返回类型

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() ,和 setIndex() .

PySide2.QtWidgets.QUndoCommand. mergeWith ( other )
参数

other QUndoCommand

返回类型

bool

Attempts to merge this command with command 。返回 true 当成功时;否则返回 false .

若此函数返回 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 command and this command.

QUndoStack will only try to merge two commands if they have the same id, and the id is not -1.

默认实现返回 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
											

另请参阅

id() push()

PySide2.QtWidgets.QUndoCommand. 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 on all child commands.

另请参阅

undo()

PySide2.QtWidgets.QUndoCommand. setObsolete ( obsolete )
参数

obsolete bool

Sets whether the command is obsolete to obsolete .

PySide2.QtWidgets.QUndoCommand. setText ( text )
参数

text – unicode

Sets the command’s text to be the text 指定。

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.

PySide2.QtWidgets.QUndoCommand. text ( )
返回类型

unicode

Returns a short text string describing what this command does; for example, “insert 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. Calling push() , undo() or redo() from this function leads to undefined beahavior.

The default implementation calls on all child commands in reverse order.

另请参阅

redo()