CopperSpice API  1.9.2
QUndoStack Class Reference

A stack of QUndoCommand objects. More...

Inheritance diagram for QUndoStack:
QObject

Public Signals

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)
 
- Public Signals inherited from QObject
void destroyed (QObject *obj=nullptr)
 
void objectNameChanged (const QString &objectName)
 

Public Slots

void redo ()
 
void setActive (bool active=true)
 
void setClean ()
 
void setIndex (int idx)
 
void undo ()
 
- Public Slots inherited from QObject
void deleteLater ()
 

Public Methods

 QUndoStack (QObject *parent=nullptr)
 
 ~QUndoStack ()
 
void beginMacro (const QString &text)
 
bool canRedo () const
 
bool canUndo () const
 
int cleanIndex () const
 
void clear ()
 
const QUndoCommandcommand (int index) const
 
int count () const
 
QActioncreateRedoAction (QObject *parent, const QString &prefix=QString ()) const
 
QActioncreateUndoAction (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
 
- Public Methods inherited from QObject
 QObject (QObject *parent=nullptr)
 
 ~QObject ()
 
bool blockSignals (bool block)
 
const QList< QObject * > & children () const
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QStringdynamicPropertyNames () const
 
virtual bool event (QEvent *event)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
template<typename T >
findChild (const QString &childName=QString ()) const
 
template<class T >
QList< T > findChildren (const QRegularExpression &regExp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<class T >
QList< T > findChildren (const QString &childName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
bool inherits (const QString &className) const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const
 
bool isWindowType () const
 
void killTimer (int id)
 
const QMetaObjectmetaObject () const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const
 
QObject * parent () const
 
template<class T = QVariant>
property (const QString &name) const
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const QString &name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
QThreadthread () const
 

Properties

 active
 
 undoLimit
 
- Properties inherited from QObject
 objectName
 

Friends

class QUndoGroup
 

Additional Inherited Members

- Static Public Methods inherited from QObject
static bool connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ())
 
static bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, std::nullptr_t, const QObject *receiver, std::nullptr_t)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...))
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod)
 
static QMetaObjectstaticMetaObject ()
 
static QString tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >())
 
- Protected Methods inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signalMethod) const
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signalMethod) const
 
bool isSignalConnected (const QMetaMethod &signalMethod) const
 
int receivers (const QString &signal) const
 
QObject * sender () const
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *event)
 

Detailed Description

The QUndoStack class is a stack of QUndoCommand objects. An undo stack maintains a stack of commands that have been applied to a document. New commands are pushed on the stack using push(). Commands can be undone and redone using undo() and redo(), or by triggering the actions returned by createUndoAction() and 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.

For an introduction refer to the Undo System documentation.

Undo and Redo Actions

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() and QUndoStack::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, see QUndoCommand::mergeWith() and 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 methods beginMacro() and 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 can not 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 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.

See also
QUndoCommand, QUndoView

Constructor & Destructor Documentation

QUndoStack::QUndoStack ( QObject parent = nullptr)
explicit

Constructs an empty undo stack with the specified parent. The stack will initially be in the clean state. If parent is a QUndoGroup object, the stack is automatically added to the group.

See also
push()
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.

See also
QUndoStack()

Method Documentation

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

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

While a macro is 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

This code is equivalent to:

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);
See also
endMacro()
bool QUndoStack::canRedo ( ) const

Returns true if there is a command available for redo, otherwise returns false. This method returns false if the stack is empty or if the top command on the stack has already been redone. Equivalent to calling index() and comparing the result to count().

See also
index(), canUndo()
void QUndoStack::canRedoChanged ( bool  canRedo)
signal

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.

bool QUndoStack::canUndo ( ) const

Returns true if there is a command available for undo, otherwise returns false. This method returns false if the stack is empty, or if the bottom command on the stack has already been undone. Equivalent to calling index() and comparing the result to zero.

See also
index(), canRedo()
void QUndoStack::canUndoChanged ( bool  canUndo)
signal

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.

void QUndoStack::cleanChanged ( bool  clean)
signal

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.

See also
isClean(), setClean()
int QUndoStack::cleanIndex ( ) const

Returns the clean index. This is the index at which setClean() 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. Since push() deletes all the undone commands before pushing the new command, the stack can not return to the clean state again. In this case, this function returns -1.

See also
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.

See also
QUndoStack()
const QUndoCommand * QUndoStack::command ( 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.

See also
QUndoCommand::child()
int QUndoStack::count ( ) const

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

See also
index(), setIndex(), command()
QAction * QUndoStack::createRedoAction ( QObject parent,
const QString prefix = QString() 
) const

Creates an redo QAction object with the given 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.

If prefix is empty, the default template "Redo %1" is used instead of prefix.

See also
createUndoAction(), canRedo(), QUndoCommand::text()
QAction * QUndoStack::createUndoAction ( QObject parent,
const QString prefix = QString() 
) const

Creates an undo QAction object with the given 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.

If prefix is empty, the default template "Undo %1" is used instead of prefix.

See also
createRedoAction(), canUndo(), QUndoCommand::text()
void QUndoStack::endMacro ( )

Ends composition of a macro command.

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

See also
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.

See also
setIndex(), undo(), redo(), count()
void QUndoStack::indexChanged ( int  idx)
signal

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

See also
index(), setIndex()
bool QUndoStack::isActive ( ) const

Returns the value of the property.

bool QUndoStack::isClean ( ) const

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

See also
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() function.

If 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() returns true, cmd is deleted.

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.

See also
QUndoCommand::id(), QUndoCommand::mergeWith()
void QUndoStack::redo ( )
slot

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.

See also
undo(), index()
QString QUndoStack::redoText ( ) const

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

See also
QUndoCommand::actionText(), undoText()
void QUndoStack::redoTextChanged ( const QString redoText)
signal

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.

void QUndoStack::setActive ( bool  active = true)
slot

Sets the value of the property to active.

void QUndoStack::setClean ( )
slot

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

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.

See also
isClean(), cleanIndex()
void QUndoStack::setIndex ( int  idx)
slot

Repeatedly calls undo() or 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.

See also
index(), count(), undo(), redo()
void QUndoStack::setUndoLimit ( int  limit)

Sets the value of the property to limit.

QString QUndoStack::text ( int  idx) const

Returns the text of the command at index idx.

See also
beginMacro()
void QUndoStack::undo ( )
slot

Undoes the command below the current command by calling QUndoCommand::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 also
redo(), index()
int QUndoStack::undoLimit ( ) const

Returns the value of the property.

QString QUndoStack::undoText ( ) const

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

See also
QUndoCommand::actionText(), redoText()
void QUndoStack::undoTextChanged ( const QString undoText)
signal

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(). The undoText specifies the new text.

Property Documentation

QUndoStack::active

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 a QUndoGroup, calls to QUndoGroup::undo() or 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.

See also
QUndoGroup
PropertiesClass Methods
read isActive
write setActive
QUndoStack::undoLimit

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

When the number of commands on a stack exceeds 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.

PropertiesClass Methods
read undoLimit
write setUndoLimit