QUndoCommand Class▲
-
Header: QUndoCommand
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
-
qmake: QT += gui
Detailed Description▲
For an overview of Qt's Undo Framework, see the overview document.
A https:// represents a single editing action on a document; for example, inserting or deleting a block of text in a text editor. https:// can apply a change to the document with https://() and undo the change with https://(). 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); }
private:
QString *m_document;
QString m_text;
};A https:// has an associated https://(). 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 https://() and https://().
https:// objects are owned by the stack they were pushed on. https:// 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 deletedIn effect, when a command is pushed, it becomes the top-most command on the stack.
To support command compression, https:// has an https://() and the virtual function https://(). These functions are used by https://().
To support command macros, a https:// 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 https://() and https://(). Instead, it uses the base implementations of these functions, which simply call https://() or https://() on all its children. The parent should, however, have a meaningful https://().
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 https://() and https://().
See Also▲
See also https://
Member Function Documentation▲
[explicit] QUndoCommand::QUndoCommand(QUndoCommand *parent = nullptr)▲
Constructs a https:// 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▲
See also https://()
[explicit] QUndoCommand::QUndoCommand(const QString &text, QUndoCommand *parent = nullptr)▲
Constructs a https:// 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▲
See also https://()
[virtual] QUndoCommand::~QUndoCommand()▲
QString QUndoCommand::actionText() const▲
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▲
See also https://(), https://(), https://(), https://()
const QUndoCommand *QUndoCommand::child(int index) const▲
int QUndoCommand::childCount() const▲
[virtual] int QUndoCommand::id() const▲
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.
https://() will only try to merge two commands if they have the same ID, and the ID is not -1.
See Also▲
See also https://(), https://()
[since 5.9] bool QUndoCommand::isObsolete() const▲
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 https://(), https://(), https://(), and https://().
This function was introduced in Qt 5.9.
See Also▲
See also https://(), https://(), https://(), https://(), https://()
[virtual] bool QUndoCommand::mergeWith(const QUndoCommand *command)▲
Attempts to merge this command with command. Returns true on success; otherwise returns false.
If this function returns true, calling this command's https://() must have the same effect as redoing both this command and command. Similarly, calling this command's https://() must have the same effect as undoing command and this command.
https:// will only try to merge two commands if they have the same id, and the id is not -1.
The default implementation returns false.
bool AppendText::mergeWith(const QUndoCommand *other)
{
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▲
See also https://(), https://()
[virtual] void QUndoCommand::redo()▲
Applies a change to the document. This function must be implemented in the derived class. Calling https://(), https://() or https://() from this function leads to undefined beahavior.
The default implementation calls redo() on all child commands.
See Also▲
See also https://()
[since 5.9] void QUndoCommand::setObsolete(bool obsolete)▲
Sets whether the command is obsolete to obsolete.
This function was introduced in Qt 5.9.
See Also▲
See also https://(), https://(), https://(), https://(), https://()
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 https://() and https://(), 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 https://() are available since Qt 4.8.
See Also▲
See also https://(), https://(), https://(), https://()
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▲
See also https://(), https://(), https://(), https://()
[virtual] void QUndoCommand::undo()▲
Reverts a change to the document. After undo() is called, the state of the document should be the same as before https://() was called. This function must be implemented in the derived class. Calling https://(), https://() or https://() from this function leads to undefined beahavior.
The default implementation calls undo() on all child commands in reverse order.
See Also▲
See also https://()


