• PySide 模块
  • PySide.QtGui
  • 内容表

    上一话题

    QMatrix2x2

    下一话题

    QDesktopServices

    QUndoCommand

    概要

    函数

    虚函数

    详细描述

    PySide.QtGui.QUndoCommand class is the base class of all commands stored on a PySide.QtGui.QUndoStack .

    有关 Qt 撤消框架的概述,见 概述文档 .

    A PySide.QtGui.QUndoCommand represents a single editing action on a document; for example, inserting or deleting a block of text in a text editor. PySide.QtGui.QUndoCommand can apply a change to the document with PySide.QtGui.QUndoCommand.redo() and undo the change with PySide.QtGui.QUndoCommand.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 PySide.QtGui.QUndoCommand has an associated PySide.QtGui.QUndoCommand.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 QUndoStack.createUndoAction() and QUndoStack.createRedoAction() .

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

    To support command macros, a PySide.QtGui.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 PySide.QtGui.QUndoCommand.undo() and PySide.QtGui.QUndoCommand.redo() . Instead, it uses the base implementations of these functions, which simply call PySide.QtGui.QUndoCommand.undo() or PySide.QtGui.QUndoCommand.redo() on all its children. The parent should, however, have a meaningful PySide.QtGui.QUndoCommand.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 QUndoStack.beginMacro() and QUndoStack.endMacro() .

    class PySide.QtGui. QUndoCommand ( [ parent=None ] )
    class PySide.QtGui. QUndoCommand ( text [ , parent=None ] )
    参数:

    构造 PySide.QtGui.QUndoCommand object with parent parent .

    parent is not 0, this command is appended to parent's child list. The parent command then owns this command and will delete it in its destructor.

    另请参阅

    ~QUndoCommand()

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

    parent is not 0, this command is appended to parent's child list. The parent command then owns this command and will delete it in its destructor.

    另请参阅

    ~QUndoCommand()

    PySide.QtGui.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.

    PySide.QtGui.QUndoCommand. child ( index )
    参数: index PySide.QtCore.int
    返回类型: PySide.QtGui.QUndoCommand

    Returns the child command at index .

    PySide.QtGui.QUndoCommand. childCount ( )
    返回类型: PySide.QtCore.int

    Returns the number of child commands in this command.

    PySide.QtGui.QUndoCommand. id ( )
    返回类型: PySide.QtCore.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.

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

    PySide.QtGui.QUndoCommand. mergeWith ( other )
    参数: other PySide.QtGui.QUndoCommand
    返回类型: PySide.QtCore.bool

    Attempts to merge this command with command . Returns true on success; otherwise returns false.

    If this function returns true, calling this command's PySide.QtGui.QUndoCommand.redo() must have the same effect as redoing both this command and command . Similarly, calling this command's PySide.QtGui.QUndoCommand.undo() must have the same effect as undoing command and this command.

    PySide.QtGui.QUndoStack will 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
    											
    PySide.QtGui.QUndoCommand. redo ( )

    Applies a change to the document. This function must be implemented in the derived class. Calling QUndoStack.push() , QUndoStack.undo() or QUndoStack.redo() from this function leads to undefined beahavior.

    默认实现调用 PySide.QtGui.QUndoCommand.redo() on all child commands.

    PySide.QtGui.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 PySide.QtGui.QUndoCommand.text() and PySide.QtGui.QUndoCommand.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 PySide.QtGui.QUndoCommand.actionText() are available since Qt 4.8.

    PySide.QtGui.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 PySide.QtGui.QUndoView .

    PySide.QtGui.QUndoCommand. undo ( )

    Reverts a change to the document. After PySide.QtGui.QUndoCommand.undo() is called, the state of the document should be the same as before PySide.QtGui.QUndoCommand.redo() was called. This function must be implemented in the derived class. Calling QUndoStack.push() , QUndoStack.undo() or QUndoStack.redo() from this function leads to undefined beahavior.

    默认实现调用 PySide.QtGui.QUndoCommand.undo() on all child commands in reverse order.