Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QUndoCommand Class Reference

The QUndoCommand class is the base class of all commands stored on a QUndoStack. More...

#include <qundostack.h>

Inheritance diagram for QUndoCommand:
Collaboration diagram for QUndoCommand:

Public Member Functions

 QUndoCommand (QUndoCommand *parent=nullptr)
 Constructs a QUndoCommand object with parent parent.
 QUndoCommand (const QString &text, QUndoCommand *parent=nullptr)
 Constructs a QUndoCommand object with the given parent and text.
virtual ~QUndoCommand ()
 Destroys the QUndoCommand object and all child commands.
virtual void undo ()
 Reverts a change to the document.
virtual void redo ()
 Applies a change to the document.
QString text () const
 Returns a short text string describing what this command does; for example, "insert text".
QString actionText () const
void setText (const QString &text)
 Sets the command's text to be the text specified.
bool isObsolete () const
void setObsolete (bool obsolete)
virtual int id () const
 Returns the ID of this command.
virtual bool mergeWith (const QUndoCommand *other)
 Attempts to merge this command with command.
int childCount () const
const QUndoCommandchild (int index) const

Friends

class QUndoStack

Detailed Description

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

Since
4.2

\inmodule QtGui

For an overview of Qt's Undo Framework, see the \l{Overview of Qt's Undo Framework}{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 : public QUndoCommand
{
public:
AppendText(QString *doc, const QString &text)
: m_document(doc), m_text(text) { setText("append text"); }
void undo() override
{ m_document->chop(m_text.length()); }
void redo() override
{ m_document->append(m_text); }
bool mergeWith(const QUndoCommand *other) override;
private:
QString *m_document;
QString 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 QUndoStack::createUndoAction() and QUndoStack::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:

MyCommand *command1 = new MyCommand();
stack->push(command1);
MyCommand *command2 = new MyCommand();
stack->push(command2);
stack->undo();
MyCommand *command3 = new 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 QUndoStack::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().

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);

Another way to create macros is to use the convenience functions QUndoStack::beginMacro() and QUndoStack::endMacro().

See also
QUndoStack

Definition at line 20 of file qundostack.h.

Constructor & Destructor Documentation

◆ QUndoCommand() [1/2]

QUndoCommand::QUndoCommand ( QUndoCommand * parent = nullptr)
explicit

Constructs a QUndoCommand object with parent parent.

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

See also
~QUndoCommand()

Definition at line 93 of file qundostack.cpp.

◆ QUndoCommand() [2/2]

QUndoCommand::QUndoCommand ( const QString & text,
QUndoCommand * parent = nullptr )
explicit

Constructs a QUndoCommand object with the given parent and text.

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

See also
~QUndoCommand()

Definition at line 77 of file qundostack.cpp.

◆ ~QUndoCommand()

QUndoCommand::~QUndoCommand ( )
virtual

Destroys the QUndoCommand object and all child commands.

See also
QUndoCommand()

Definition at line 106 of file qundostack.cpp.

Member Function Documentation

◆ actionText()

QString QUndoCommand::actionText ( ) const
Since
4.8

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.

See also
text(), setText(), QUndoStack::createUndoAction(), QUndoStack::createRedoAction()

Definition at line 248 of file qundostack.cpp.

◆ child()

const QUndoCommand * QUndoCommand::child ( int index) const
Since
4.4

Returns the child command at index.

See also
childCount(), QUndoStack::command()

Definition at line 301 of file qundostack.cpp.

◆ childCount()

int QUndoCommand::childCount ( ) const
Since
4.4

Returns the number of child commands in this command.

See also
child()

Definition at line 288 of file qundostack.cpp.

◆ id()

int QUndoCommand::id ( ) const
virtual

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.

See also
mergeWith(), QUndoStack::push()

Reimplemented in MyCommand, MyCommand, and qdesigner_internal::SetPropertyCommand.

Definition at line 157 of file qundostack.cpp.

◆ isObsolete()

bool QUndoCommand::isObsolete ( ) const
Since
5.9

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 isObsolete function is checked in the functions QUndoStack::push(), QUndoStack::undo(), QUndoStack::redo(), and QUndoStack::setIndex().

See also
setObsolete(), mergeWith(), QUndoStack::push(), QUndoStack::undo(), QUndoStack::redo()

Definition at line 124 of file qundostack.cpp.

◆ mergeWith()

bool QUndoCommand::mergeWith ( const QUndoCommand * command)
virtual

Attempts to merge this command with command.

Returns true on success; otherwise returns false.

If this function returns 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.

The default implementation returns false.

{
if (other->id() != id()) // make sure other is also an AppendText command
return false;
m_text += static_cast<const AppendText*>(other)->m_text;
return true;
}
See also
id(), QUndoStack::push()

Reimplemented in AppendText, AppendText, qdesigner_internal::CECommand, qdesigner_internal::ResetPropertyCommand, and qdesigner_internal::SetPropertyCommand.

Definition at line 181 of file qundostack.cpp.

◆ redo()

void QUndoCommand::redo ( )
virtual

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.

The default implementation calls redo() on all child commands.

See also
undo()

Reimplemented in AppendText, AppendText, MyCommand, MyCommand, qdesigner_internal::AddActionCommand, qdesigner_internal::AddButtonsToGroupCommand, qdesigner_internal::AddButtonsToGroupCommand, qdesigner_internal::AddConnectionCommand, qdesigner_internal::AddContainerWidgetPageCommand, qdesigner_internal::AddDockWidgetCommand, qdesigner_internal::AddDynamicPropertyCommand, qdesigner_internal::AddMenuActionCommand, qdesigner_internal::AddStackedWidgetPageCommand, qdesigner_internal::AddTabPageCommand, qdesigner_internal::AddToolBarCommand, qdesigner_internal::AddToolBoxPageCommand, qdesigner_internal::AdjustConnectionCommand, qdesigner_internal::AdjustConnectionCommand, qdesigner_internal::AdjustWidgetSizeCommand, qdesigner_internal::BreakButtonGroupCommand, qdesigner_internal::BreakButtonGroupCommand, qdesigner_internal::BreakLayoutCommand, qdesigner_internal::ChangeCurrentPageCommand, qdesigner_internal::ChangeFormLayoutItemRoleCommand, qdesigner_internal::ChangeLayoutItemGeometry, qdesigner_internal::ChangeListContentsCommand, qdesigner_internal::ChangeTableContentsCommand, qdesigner_internal::ChangeTreeContentsCommand, qdesigner_internal::ChangeZOrderCommand, qdesigner_internal::CreateButtonGroupCommand, qdesigner_internal::CreateButtonGroupCommand, qdesigner_internal::CreateMenuBarCommand, qdesigner_internal::CreateStatusBarCommand, qdesigner_internal::CreateSubmenuCommand, qdesigner_internal::DeleteConnectionsCommand, qdesigner_internal::DeleteContainerWidgetPageCommand, qdesigner_internal::DeleteMenuBarCommand, qdesigner_internal::DeleteStackedWidgetPageCommand, qdesigner_internal::DeleteStatusBarCommand, qdesigner_internal::DeleteTabPageCommand, qdesigner_internal::DeleteToolBarCommand, qdesigner_internal::DeleteToolBoxPageCommand, qdesigner_internal::DeleteWidgetCommand, qdesigner_internal::DemoteFromCustomWidgetCommand, qdesigner_internal::InsertActionIntoCommand, qdesigner_internal::InsertWidgetCommand, qdesigner_internal::LayoutAlignmentCommand, qdesigner_internal::LayoutCommand, qdesigner_internal::ModifyConnectionCommand, qdesigner_internal::ModifyConnectionCommand, qdesigner_internal::MorphLayoutCommand, qdesigner_internal::MorphWidgetCommand, qdesigner_internal::MorphWidgetCommand, qdesigner_internal::MoveStackedWidgetCommand, qdesigner_internal::MoveTabPageCommand, qdesigner_internal::MoveToolBoxPageCommand, qdesigner_internal::PromoteToCustomWidgetCommand, qdesigner_internal::QDesignerFormWindowCommand, qdesigner_internal::RemoveActionCommand, qdesigner_internal::RemoveActionFromCommand, qdesigner_internal::RemoveButtonsFromGroupCommand, qdesigner_internal::RemoveButtonsFromGroupCommand, qdesigner_internal::RemoveDynamicPropertyCommand, qdesigner_internal::RemoveMenuActionCommand, qdesigner_internal::ReparentWidgetCommand, qdesigner_internal::ResetPropertyCommand, qdesigner_internal::SetEndPointCommand, qdesigner_internal::SetEndPointCommand, qdesigner_internal::SetMemberCommand, qdesigner_internal::SetMemberCommand, qdesigner_internal::SetPropertyCommand, qdesigner_internal::SimplifyLayoutCommand, qdesigner_internal::TabOrderCommand, SetControlCommand, and SetControlCommand.

Definition at line 198 of file qundostack.cpp.

◆ setObsolete()

void QUndoCommand::setObsolete ( bool obsolete)
Since
5.9

Sets whether the command is obsolete to obsolete.

See also
isObsolete(), mergeWith(), QUndoStack::push(), QUndoStack::undo(), QUndoStack::redo()

Definition at line 137 of file qundostack.cpp.

◆ setText()

void QUndoCommand::setText ( const QString & text)

Sets the command's text to be the text specified.

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.

See also
text(), actionText(), QUndoStack::createUndoAction(), QUndoStack::createRedoAction()

Definition at line 268 of file qundostack.cpp.

◆ text()

QString QUndoCommand::text ( ) const

Returns a short text string describing what this command does; for example, "insert text".

The text is used for names of items in QUndoView.

See also
actionText(), setText(), QUndoStack::createUndoAction(), QUndoStack::createRedoAction()

Definition at line 231 of file qundostack.cpp.

◆ undo()

void QUndoCommand::undo ( )
virtual

Reverts a change to the document.

After undo() 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 QUndoStack::push(), QUndoStack::undo() or QUndoStack::redo() from this function leads to undefined beahavior.

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

See also
redo()

Reimplemented in AppendText, AppendText, MyCommand, MyCommand, qdesigner_internal::AddActionCommand, qdesigner_internal::AddButtonsToGroupCommand, qdesigner_internal::AddButtonsToGroupCommand, qdesigner_internal::AddConnectionCommand, qdesigner_internal::AddContainerWidgetPageCommand, qdesigner_internal::AddDockWidgetCommand, qdesigner_internal::AddDynamicPropertyCommand, qdesigner_internal::AddMenuActionCommand, qdesigner_internal::AddStackedWidgetPageCommand, qdesigner_internal::AddTabPageCommand, qdesigner_internal::AddToolBarCommand, qdesigner_internal::AddToolBoxPageCommand, qdesigner_internal::AdjustConnectionCommand, qdesigner_internal::AdjustConnectionCommand, qdesigner_internal::AdjustWidgetSizeCommand, qdesigner_internal::BreakButtonGroupCommand, qdesigner_internal::BreakButtonGroupCommand, qdesigner_internal::BreakLayoutCommand, qdesigner_internal::ChangeCurrentPageCommand, qdesigner_internal::ChangeFormLayoutItemRoleCommand, qdesigner_internal::ChangeLayoutItemGeometry, qdesigner_internal::ChangeListContentsCommand, qdesigner_internal::ChangeTableContentsCommand, qdesigner_internal::ChangeTreeContentsCommand, qdesigner_internal::ChangeZOrderCommand, qdesigner_internal::CreateButtonGroupCommand, qdesigner_internal::CreateButtonGroupCommand, qdesigner_internal::CreateMenuBarCommand, qdesigner_internal::CreateStatusBarCommand, qdesigner_internal::CreateSubmenuCommand, qdesigner_internal::DeleteConnectionsCommand, qdesigner_internal::DeleteContainerWidgetPageCommand, qdesigner_internal::DeleteMenuBarCommand, qdesigner_internal::DeleteStackedWidgetPageCommand, qdesigner_internal::DeleteStatusBarCommand, qdesigner_internal::DeleteTabPageCommand, qdesigner_internal::DeleteToolBarCommand, qdesigner_internal::DeleteToolBoxPageCommand, qdesigner_internal::DeleteWidgetCommand, qdesigner_internal::DemoteFromCustomWidgetCommand, qdesigner_internal::InsertActionIntoCommand, qdesigner_internal::InsertWidgetCommand, qdesigner_internal::LayoutAlignmentCommand, qdesigner_internal::LayoutCommand, qdesigner_internal::ModifyConnectionCommand, qdesigner_internal::ModifyConnectionCommand, qdesigner_internal::MorphLayoutCommand, qdesigner_internal::MorphWidgetCommand, qdesigner_internal::MorphWidgetCommand, qdesigner_internal::MoveStackedWidgetCommand, qdesigner_internal::MoveTabPageCommand, qdesigner_internal::MoveToolBoxPageCommand, qdesigner_internal::PromoteToCustomWidgetCommand, qdesigner_internal::PropertyListCommand, qdesigner_internal::QDesignerFormWindowCommand, qdesigner_internal::RemoveActionCommand, qdesigner_internal::RemoveActionFromCommand, qdesigner_internal::RemoveButtonsFromGroupCommand, qdesigner_internal::RemoveButtonsFromGroupCommand, qdesigner_internal::RemoveDynamicPropertyCommand, qdesigner_internal::RemoveMenuActionCommand, qdesigner_internal::ReparentWidgetCommand, qdesigner_internal::SetEndPointCommand, qdesigner_internal::SetEndPointCommand, qdesigner_internal::SetMemberCommand, qdesigner_internal::SetMemberCommand, qdesigner_internal::SimplifyLayoutCommand, qdesigner_internal::TabOrderCommand, SetControlCommand, and SetControlCommand.

Definition at line 216 of file qundostack.cpp.

◆ QUndoStack

friend class QUndoStack
friend

Definition at line 47 of file qundostack.h.


The documentation for this class was generated from the following files: