The QUndoCommand class is the base class of all commands stored on a QUndoStack . 更多...
| 頭: | #include <QUndoCommand> |
| qmake: | QT += widgets |
| Since: | Qt 4.2 |
| QUndoCommand (QUndoCommand * parent = Q_NULLPTR) | |
| QUndoCommand (const QString & text , QUndoCommand * parent = Q_NULLPTR) | |
| virtual | ~QUndoCommand () |
| QString | actionText () const |
| const QUndoCommand * | child (int index ) const |
| int | childCount () const |
| virtual int | id () const |
| bool | isObsolete () const |
| virtual bool | mergeWith (const QUndoCommand * command ) |
| virtual void | redo () |
| void | setObsolete (bool obsolete ) |
| void | setText (const QString & text ) |
| QString | text () const |
| virtual void | undo () |
The QUndoCommand class is the base class of all commands stored on a QUndoStack .
有關 Qt 撤消框架的概述,見 概述文檔 .
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"); } virtual void undo() { m_document->chop(m_text.length()); } virtual void redo() { m_document->append(m_text); } 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 () 和 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 () 和虛函數 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 () 和 redo (). Instead, it uses the base implementations of these functions, which simply call undo () 或 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 () 和 QUndoStack::endMacro ().
另請參閱 QUndoStack .
構造 QUndoCommand object with parent parent .
若 parent is not 0, this command is appended to parent's child list. The parent command then owns this command and will delete it in its destructor.
另請參閱 ~QUndoCommand ().
構造 QUndoCommand 對象采用給定 parent and text .
若 parent is not 0, this command is appended to parent's child list. The parent command then owns this command and will delete it in its destructor.
另請參閱 ~QUndoCommand ().
[虛擬]
QUndoCommand::
~QUndoCommand
()
銷毀 QUndoCommand 對象和所有子級命令。
另請參閱 QUndoCommand ().
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.
該函數在 Qt 4.8 引入。
另請參閱 text (), setText (), QUndoStack::createUndoAction (),和 QUndoStack::createRedoAction ().
返迴子級命令在 index .
該函數在 Qt 4.4 引入。
另請參閱 childCount () 和 QUndoStack::command ().
返迴此命令中的子級命令數。
該函數在 Qt 4.4 引入。
另請參閱 child ().
[虛擬]
int
QUndoCommand::
id
() const
返迴此命令的 ID。
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.
另請參閱 mergeWith () 和 QUndoStack::push ().
返迴命令是否已過時。
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 (),和 QUndoStack::setIndex ().
該函數在 Qt 5.9 引入。
另請參閱 setObsolete (), mergeWith (), QUndoStack::push (), QUndoStack::undo (),和 QUndoStack::redo ().
[虛擬]
bool
QUndoCommand::
mergeWith
(const
QUndoCommand
*
command
)
試圖閤並此命令同
command
。返迴
true
當成功時;否則返迴
false
.
若此函數返迴
true
,調用此命令的
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 隻會試著閤並 2 個命令,若它們擁有相同 ID 且 ID 不是 -1。
默認實現返迴
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; }
另請參閱 id () 和 QUndoStack::push ().
[虛擬]
void
QUndoCommand::
redo
()
Applies a change to the document. This function must be implemented in the derived class. Calling QUndoStack::push (), QUndoStack::undo () 或 QUndoStack::redo () from this function leads to undefined beahavior.
默認實現在所有子級命令中調用 redo()。
另請參閱 undo ().
將命令是否過時設為 obsolete .
該函數在 Qt 5.9 引入。
另請參閱 isObsolete (), mergeWith (), QUndoStack::push (), QUndoStack::undo (),和 QUndoStack::redo ().
將命令文本設為 text 指定。
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 () 和 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.
另請參閱 text (), actionText (), QUndoStack::createUndoAction (),和 QUndoStack::createRedoAction ().
Returns a short text string describing what this command does; for example, "insert text".
The text is used for names of items in QUndoView .
另請參閱 actionText (), setText (), QUndoStack::createUndoAction (),和 QUndoStack::createRedoAction ().
[虛擬]
void
QUndoCommand::
undo
()
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 () 或 QUndoStack::redo () from this function leads to undefined beahavior.
The default implementation calls undo() on all child commands in reverse order.
另請參閱 redo ().