Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QUndoStack Class

TheQUndoStack class is a stack ofQUndoCommand objects.More...

Header:#include <QUndoStack>
Since: Qt 4.2
Inherits:QObject

Properties

Public Functions

QUndoStack(QObject * parent = 0)
~QUndoStack()
voidbeginMacro(const QString & text)
boolcanRedo() const
boolcanUndo() const
intcleanIndex() const
voidclear()
const QUndoCommand *command(int index) const
intcount() const
QAction *createRedoAction(QObject * parent, const QString & prefix = QString()) const
QAction *createUndoAction(QObject * parent, const QString & prefix = QString()) const
voidendMacro()
intindex() const
boolisActive() const
boolisClean() const
voidpush(QUndoCommand * cmd)
QStringredoText() const
voidsetUndoLimit(int limit)
QStringtext(int idx) const
intundoLimit() const
QStringundoText() const
  • 29 public functions inherited fromQObject

Public Slots

voidredo()
voidsetActive(bool active = true)
voidsetClean()
voidsetIndex(int idx)
voidundo()
  • 1 public slot inherited fromQObject

Signals

voidcanRedoChanged(bool canRedo)
voidcanUndoChanged(bool canUndo)
voidcleanChanged(bool clean)
voidindexChanged(int idx)
voidredoTextChanged(const QString & redoText)
voidundoTextChanged(const QString & undoText)

Additional Inherited Members

  • 7 static public members inherited fromQObject
  • 8 protected functions inherited fromQObject

Detailed Description

TheQUndoStack class is a stack ofQUndoCommand objects.

For an overview of Qt's Undo Framework, see theoverview document.

An undo stack maintains a stack of commands that have been applied to a document.

New commands are pushed on the stack usingpush(). Commands can be undone and redone usingundo() andredo(), or by triggering the actions returned bycreateUndoAction() andcreateRedoAction().

QUndoStack keeps track of thecurrent command. This is the command which will be executed by the next call toredo(). The index of this command is returned byindex(). The state of the edited object can be rolled forward or back usingsetIndex(). If the top-most command on the stack has already been redone,index() is equal tocount().

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

Undo and Redo Actions

QUndoStack provides convenient undo and redoQAction 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 byQUndoStack::createUndoAction() andQUndoStack::createRedoAction().

Command Compression and Macros

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, seeQUndoCommand::mergeWith() andpush().

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 theQUndoCommand constructor, or by using the convenience functionsbeginMacro() andendMacro().

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.

Clean State

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

See alsoQUndoCommand andQUndoView.

Property Documentation

active :bool

This property holds the active status of this stack.

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 aQUndoGroup, calls toQUndoGroup::undo() orQUndoGroup::redo() will be forwarded to this stack when it is active. If theQUndoGroup is watched by aQUndoView, the view will display the contents of this stack when it is active. If the stack does not belong to aQUndoGroup, 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.

Access functions:

boolisActive() const
voidsetActive(bool active = true)

See alsoQUndoGroup.

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.

This property was introduced in Qt 4.3.

Access functions:

intundoLimit() const
voidsetUndoLimit(int limit)

Member Function Documentation

QUndoStack::QUndoStack(QObject * parent = 0)

Constructs an empty undo stack with the parentparent. The stack will initially be in the clean state. Ifparent is aQUndoGroup object, the stack is automatically added to the group.

See alsopush().

QUndoStack::~QUndoStack()

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

See alsoQUndoStack().

void QUndoStack::beginMacro(constQString & text)

Begins composition of a macro command with the giventext description.

An empty command described by the specifiedtext is pushed on the stack. Any subsequent commands pushed on the stack will be appended to the empty command's children untilendMacro() is called.

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

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

The stack becomes enabled and appropriate signals are emitted whenendMacro() 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

This code is equivalent to:

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

See alsoendMacro().

bool QUndoStack::canRedo() const

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

This function returns false if the stack is empty or if the top command on the stack has already been redone.

Synonymous withindex() ==count().

See alsoindex() andcanUndo().

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

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

bool QUndoStack::canUndo() const

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

This function returns false if the stack is empty, or if the bottom command on the stack has already been undone.

Synonymous withindex() == 0.

See alsoindex() andcanRedo().

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

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

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

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

See alsoisClean() andsetClean().

int QUndoStack::cleanIndex() const

Returns the clean index. This is the index at whichsetClean() was called.

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

See alsoisClean() andsetClean().

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.

See alsoQUndoStack().

constQUndoCommand * QUndoStack::command(int index) const

Returns a const pointer to the command atindex.

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.

This function was introduced in Qt 4.4.

See alsoQUndoCommand::child().

int QUndoStack::count() const

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

See alsoindex(),setIndex(), andcommand().

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

Creates an redoQAction object with the givenparent.

Triggering this action will cause a call toredo(). The text of this action is the text of the command which will be redone in the next call toredo(), prefixed by the specifiedprefix. If there is no command available for redo, this action will be disabled.

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

See alsocreateUndoAction(),canRedo(), andQUndoCommand::text().

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

Creates an undoQAction object with the givenparent.

Triggering this action will cause a call toundo(). The text of this action is the text of the command which will be undone in the next call toundo(), prefixed by the specifiedprefix. If there is no command available for undo, this action will be disabled.

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

See alsocreateRedoAction(),canUndo(), andQUndoCommand::text().

void QUndoStack::endMacro()

Ends composition of a macro command.

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

See alsobeginMacro().

int QUndoStack::index() const

Returns the index of the current command. This is the command that will be executed on the next call toredo(). It is not always the top-most command on the stack, since a number of commands may have been undone.

See alsosetIndex(),undo(),redo(), andcount().

[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, orsetIndex() 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 toredo().

See alsoindex() andsetIndex().

bool QUndoStack::isClean() const

If the stack is in the clean state, returns true; otherwise returns false.

See alsosetClean() andcleanIndex().

void QUndoStack::push(QUndoCommand * cmd)

Pushescmd on the stack or merges it with the most recently executed command. In either case, executescmd by calling itsredo() function.

Ifcmd'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 callingQUndoCommand::mergeWith() on the most recently executed command. IfQUndoCommand::mergeWith() returns true,cmd is deleted.

In all other casescmd is simply pushed on the stack.

If commands were undone beforecmd was pushed, the current command and all commands above it are deleted. Hencecmd 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.

See alsoQUndoCommand::id() andQUndoCommand::mergeWith().

[slot]void QUndoStack::redo()

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

See alsoundo() andindex().

QString QUndoStack::redoText() const

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

See alsoQUndoCommand::actionText() andundoText().

[signal]void QUndoStack::redoTextChanged(constQString & redoText)

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

[slot]void QUndoStack::setClean()

Marks the stack as clean and emitscleanChanged() if the stack was not already clean.

Whenever the stack returns to this state through the use of undo/redo commands, it emits the signalcleanChanged(). This signal is also emitted when the stack leaves the clean state.

See alsoisClean() andcleanIndex().

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

Repeatedly callsundo() orredo() until the current command index reachesidx. This function can be used to roll the state of the document forwards of backwards.indexChanged() is emitted only once.

See alsoindex(),count(),undo(), andredo().

QString QUndoStack::text(int idx) const

Returns the text of the command at indexidx.

See alsobeginMacro().

[slot]void QUndoStack::undo()

Undoes the command below the current command by callingQUndoCommand::undo(). Decrements the current command index.

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

See alsoredo() andindex().

QString QUndoStack::undoText() const

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

See alsoQUndoCommand::actionText() andredoText().

[signal]void QUndoStack::undoTextChanged(constQString & undoText)

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

© 2016 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 theGNU 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.


[8]ページ先頭

©2009-2025 Movatter.jp