Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
src_gui_util_qundostack.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
5class AppendText : public QUndoCommand
6{
7public:
9 : m_document(doc), m_text(text) { setText("append text"); }
10 void undo() override
11 { m_document->chop(m_text.length()); }
12 void redo() override
13 { m_document->append(m_text); }
14private:
15 QString *m_document;
16 QString m_text;
17};
19
20
22MyCommand *command1 = new MyCommand();
23stack->push(command1);
24MyCommand *command2 = new MyCommand();
25stack->push(command2);
26
27stack->undo();
28
29MyCommand *command3 = new MyCommand();
30stack->push(command3); // command2 gets deleted
32
33
35QUndoCommand *insertRed = new QUndoCommand(); // an empty command
36insertRed->setText("insert red text");
37
38new InsertText(document, idx, text, insertRed); // becomes child of insertRed
39new SetColor(document, idx, text.length(), Qt::red, insertRed);
40
41stack.push(insertRed);
43
44
47{
48 if (other->id() != id()) // make sure other is also an AppendText command
49 return false;
50 m_text += static_cast<const AppendText*>(other)->m_text;
51 return true;
52}
54
55
57stack.beginMacro("insert red text");
58stack.push(new InsertText(document, idx, text));
59stack.push(new SetColor(document, idx, text.length(), Qt::red));
60stack.endMacro(); // indexChanged() is emitted
62
63
65QUndoCommand *insertRed = new QUndoCommand(); // an empty command
66insertRed->setText("insert red text");
67
68new InsertText(document, idx, text, insertRed); // becomes child of insertRed
69new SetColor(document, idx, text.length(), Qt::red, insertRed);
70
71stack.push(insertRed);
AppendText(QString *doc, const QString &text)
void undo() override
Reverts a change to the document.
void redo() override
Applies a change to the document.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
void chop(qsizetype n)
Removes n characters from the end of the string.
Definition qstring.cpp:6340
QString & append(QChar c)
Definition qstring.cpp:3252
qsizetype length() const noexcept
Returns the number of characters in this string.
Definition qstring.h:191
virtual bool mergeWith(const QUndoCommand *other)
Attempts to merge this command with command.
void setText(const QString &text)
Sets the command's text to be the text specified.
QString text() const
Returns a short text string describing what this command does; for example, "insert text".
QString text
@ red
Definition qnamespace.h:35
QSharedPointer< T > other(t)
[5]
MyCommand * command2
new SetColor(document, idx, text.length(), Qt::red, insertRed)
QUndoCommand * insertRed
[1]
MyCommand * command3
new InsertText(document, idx, text, insertRed)
MyCommand * command1
[0]