QUndoStack 类

QUndoStack 类是堆栈为 QUndoCommand 对象。 更多...

头: #include <QUndoStack>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui
继承: QObject

特性

公共函数

QUndoStack (QObject * parent = nullptr)
virtual ~QUndoStack ()
void beginMacro (const QString & text )
bool canRedo () const
bool canUndo () const
int cleanIndex () const
void clear ()
const QUndoCommand * 命令 (int index ) const
int count () const
QAction * createRedoAction (QObject * parent , const QString & prefix = QString()) const
QAction * createUndoAction (QObject * parent , const QString & prefix = QString()) const
void endMacro ()
int index () const
bool isActive () const
bool isClean () const
void push (QUndoCommand * cmd )
QString redoText () const
void setUndoLimit (int limit )
QString text (int idx ) const
int undoLimit () const
QString undoText () const

公共槽

void redo ()
void resetClean ()
void setActive (bool active = true)
void setClean ()
void setIndex (int idx )
void undo ()

信号

void canRedoChanged (bool canRedo )
void canUndoChanged (bool canUndo )
void cleanChanged (bool clean )
void indexChanged (int idx )
void redoTextChanged (const QString & redoText )
void undoTextChanged (const QString & undoText )

详细描述

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

撤消堆栈维护已应用于文档的命令堆栈。

将新命令压入堆栈是使用 push (). 命令的撤消和重做可以使用 undo () 和 redo (),或通过触发动作返回通过 createUndoAction () 和 createRedoAction ().

QUndoStack keeps track of the current command. This is the command which will be executed by the next call to redo (). The index of this command is returned by index (). The state of the edited object can be rolled forward or back using setIndex (). If the top-most command on the stack has already been redone, index () is equal to count ().

QUndoStack provides support for undo and redo actions, command compression, command macros, and supports the concept of a clean state .

撤消和重做动作

QUndoStack provides convenient undo and redo QAction 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 by QUndoStack::createUndoAction () 和 QUndoStack::createRedoAction ().

命令压缩和宏

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 QUndoCommand::mergeWith () 和 push ().

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 functions beginMacro () 和 endMacro ().

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.

清理状态

QUndoStack supports the concept of a clean state. When the document is saved to disk, the stack can be marked as clean using setClean (). Whenever the stack returns to this state through undoing and redoing commands, it emits the signal cleanChanged (). 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.

过时命令

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 the redo () 和 undo () functions have no function since there was connection issues.

A command can be marked obsolete with the QUndoCommand::setObsolete () 函数。 QUndoCommand::isObsolete () flag is checked in QUndoStack::push (), QUndoStack::undo (), QUndoStack::redo (),和 QUndoStack::setIndex () after calling QUndoCommand::undo (), QUndoCommand::redo () 和 QUndoCommand :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.

另请参阅 QUndoCommand and QUndoView .

特性文档编制

active : bool

此特性保持此堆栈的活动状态。

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 to QUndoGroup::undo () 或 QUndoGroup::redo () will be forwarded to this stack when it is active. If the QUndoGroup is watched by a QUndoView , the view will display the contents of this stack when it is active. If the stack does not belong to a QUndoGroup , making it active has no effect.

It is the programmer's responsibility to specify which stack is active by calling setActive(), usually when the associated document window receives focus.

访问函数:

bool isActive () const
void setActive (bool active = true)

另请参阅 QUndoGroup .

[read-only] canRedo : const bool

This property holds whether this stack can redo.

This property indicates whether or not there is a command that can be redone.

访问函数:

bool canRedo () const

通知程序信号:

void canRedoChanged (bool canRedo )

另请参阅 canRedo (), index (),和 canUndo ().

[read-only] canUndo : const bool

This property holds whether this stack can undo.

This property indicates whether or not there is a command that can be undone.

访问函数:

bool canUndo () const

通知程序信号:

void canUndoChanged (bool canUndo )

另请参阅 canUndo (), index (),和 canRedo ().

[read-only] clean : const 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.

访问函数:

bool isClean () const

通知程序信号:

void cleanChanged (bool clean )

另请参阅 isClean (), setClean (), resetClean (),和 cleanIndex ().

[read-only] redoText : const QString

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 ().

访问函数:

QString redoText () const

通知程序信号:

void redoTextChanged (const QString & redoText )

另请参阅 redoText (), QUndoCommand::actionText (),和 undoText ().

undoLimit : int

This property holds the maximum number of commands on this stack.

When the number of commands on a stack exceedes the stack's undoLimit, 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 setUndoLimit() on a non-empty stack prints a warning and does nothing.

访问函数:

int undoLimit () const
void setUndoLimit (int limit )

[read-only] undoText : const QString

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 ().

访问函数:

QString undoText () const

通知程序信号:

void undoTextChanged (const QString & undoText )

另请参阅 undoText (), QUndoCommand::actionText (),和 redoText ().

成员函数文档编制

[explicit] QUndoStack:: QUndoStack ( QObject * parent = nullptr)

构造空撤消堆栈采用父级 parent . The stack will initially be in the clean state. If parent QUndoGroup object, the stack is automatically added to the group.

另请参阅 push ().

[virtual noexcept] QUndoStack:: ~QUndoStack ()

Destroys the undo stack, deleting any commands that are on it. If the stack is in a QUndoGroup , the stack is automatically removed from the group.

另请参阅 QUndoStack ().

void QUndoStack:: beginMacro (const QString & text )

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 until endMacro () 被调用。

Calls to beginMacro() and endMacro () may be nested, but every call to beginMacro() must have a matching call to endMacro ().

While a macro is being composed, the stack is disabled. This means that:

The stack becomes enabled and appropriate signals are emitted when endMacro () is called for the outermost macro.

stack.beginMacro("insert red text");
stack.push(new InsertText(document, idx, text));
stack.push(new SetColor(document, idx, text.length(), Qt::red));
stack.endMacro(); // indexChanged() is emitted
					

此代码相当于:

QUndoCommand *insertRed = new QUndoCommand(); // an empty command
insertRed->setText("insert red text");
new InsertText(document, idx, text, insertRed); // becomes child of insertRed
new SetColor(document, idx, text.length(), Qt::red, insertRed);
stack.push(insertRed);
					

另请参阅 endMacro ().

bool QUndoStack:: canRedo () const

返回 true if there is a command available for redo; otherwise returns false .

此函数返回 false if the stack is empty or if the top command on the stack has already been redone.

Synonymous with index () == count ().

注意: Getter function for property canRedo.

另请参阅 index () 和 canUndo ().

[signal] void QUndoStack:: canRedoChanged ( bool canRedo )

This signal is emitted whenever the value of canRedo () changes. It is used to enable or disable the redo action returned by createRedoAction (). canRedo specifies the new value.

注意: 通知程序信号对于特性 canRedo .

bool QUndoStack:: canUndo () const

返回 true if there is a command available for undo; otherwise returns false .

此函数返回 false if the stack is empty, or if the bottom command on the stack has already been undone.

Synonymous with index () == 0.

注意: Getter 函数对于特性 canUndo。

另请参阅 index () 和 canRedo ().

[signal] void QUndoStack:: canUndoChanged ( bool canUndo )

This signal is emitted whenever the value of canUndo () changes. It is used to enable or disable the undo action returned by createUndoAction (). canUndo specifies the new value.

注意: 通知程序信号对于特性 canUndo .

[signal] void QUndoStack:: cleanChanged ( bool clean )

This signal is emitted whenever the stack enters or leaves the clean state. If clean is true, the stack is in a clean state; otherwise this signal indicates that it has left the clean state.

注意: 通知程序信号对于特性 clean .

另请参阅 isClean () 和 setClean ().

int QUndoStack:: cleanIndex () const

Returns the clean index. This is the index at which setClean () 被调用。

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 to resetClean ().

另请参阅 isClean () 和 setClean ().

void 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.

另请参阅 QUndoStack ().

const QUndoCommand *QUndoStack:: 命令 ( int index ) const

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.

另请参阅 QUndoCommand::child ().

int QUndoStack:: count () const

Returns the number of commands on the stack. Macro commands are counted as one command.

另请参阅 index (), setIndex (),和 命令 ().

QAction *QUndoStack:: createRedoAction ( QObject * parent , const QString & prefix = QString()) const

创建重做 QAction 对象采用给定 parent .

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 to redo (), prefixed by the specified prefix . If there is no command available for redo, this action will be disabled.

prefix is empty, the default template "Redo %1" is used instead of prefix. Before Qt 4.8, the prefix "Redo" was used by default.

另请参阅 createUndoAction (), canRedo (),和 QUndoCommand::text ().

QAction *QUndoStack:: createUndoAction ( QObject * parent , const QString & prefix = QString()) const

创建撤消 QAction 对象采用给定 parent .

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 to undo (), prefixed by the specified prefix . If there is no command available for undo, this action will be disabled.

prefix is empty, the default template "Undo %1" is used instead of prefix. Before Qt 4.8, the prefix "Undo" was used by default.

另请参阅 createRedoAction (), canUndo (),和 QUndoCommand::text ().

void QUndoStack:: endMacro ()

结束宏命令的合成。

If this is the outermost macro in a set nested macros, this function emits indexChanged () once for the entire macro command.

另请参阅 beginMacro ().

int QUndoStack:: index () const

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.

另请参阅 setIndex (), undo (), redo (),和 count ().

[signal] void QUndoStack:: indexChanged ( int idx )

This signal is emitted whenever a command modifies the state of the document. This happens when a command is undone or redone. When a macro command is undone or redone, or setIndex () is called, this signal is emitted only once.

idx specifies the index of the current command, ie. the command which will be executed on the next call to redo ().

另请参阅 index () 和 setIndex ().

bool QUndoStack:: isClean () const

若堆栈处于清理状态,返回 true ;否则返回 false .

注意: getter 函数对于特性 clean .

另请参阅 setClean () 和 cleanIndex ().

void QUndoStack:: push ( QUndoCommand * cmd )

Pushes cmd on the stack or merges it with the most recently executed command. In either case, executes cmd by calling its redo () 函数。

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 calling QUndoCommand::mergeWith () on the most recently executed command. If QUndoCommand::mergeWith () 返回 true , cmd 被删除。

After calling QUndoCommand::redo () and, if applicable, QUndoCommand::mergeWith (), QUndoCommand::isObsolete () will be called for cmd or the merged command. If QUndoCommand::isObsolete () 返回 true ,那么 cmd 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. Hence cmd 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.

另请参阅 QUndoCommand::id () 和 QUndoCommand::mergeWith ().

[slot] void QUndoStack:: redo ()

Redoes the current command by calling QUndoCommand::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.

QUndoCommand::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.

另请参阅 undo () 和 index ().

QString QUndoStack:: redoText () const

Returns the text of the command which will be redone in the next call to redo ().

注意: getter 函数对于特性 redoText。

另请参阅 QUndoCommand::actionText () 和 undoText ().

[signal] void QUndoStack:: redoTextChanged (const QString & redoText )

This signal is emitted whenever the value of redoText () changes. It is used to update the text property of the redo action returned by createRedoAction (). redoText specifies the new text.

注意: 通知程序信号对于特性 redoText .

[slot] void 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.

另请参阅 isClean (), setClean (),和 cleanIndex ().

[slot] void 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.

另请参阅 isClean (), resetClean (),和 cleanIndex ().

[slot] void QUndoStack:: setIndex ( int idx )

Repeatedly calls undo () 或 redo () until the current command index reaches idx . This function can be used to roll the state of the document forwards of backwards. indexChanged () is emitted only once.

另请参阅 index (), count (), undo (),和 redo ().

QString QUndoStack:: text ( int idx ) const

Returns the text of the command at index idx .

另请参阅 beginMacro ().

[slot] void QUndoStack:: undo ()

Undoes the command below the current command by calling QUndoCommand::undo ()。递减当前命令索引。

If the stack is empty, or if the bottom command on the stack has already been undone, this function does nothing.

在撤消命令后,若 QUndoCommand::isObsolete () 返回 true , 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.

另请参阅 redo () 和 index ().

QString QUndoStack:: undoText () const

Returns the text of the command which will be undone in the next call to undo ().

注意: Getter 函数对于特性 undoText。

另请参阅 QUndoCommand::actionText () 和 redoText ().

[signal] void QUndoStack:: undoTextChanged (const QString & undoText )

This signal is emitted whenever the value of undoText () changes. It is used to update the text property of the undo action returned by createUndoAction (). undoText specifies the new text.

注意: 通知程序信号对于特性 undoText .