<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html><head><title>QUndoCommand Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm } a:link { color: #004faf; text-decoration: none } a:visited { color: #672967; text-decoration: none } td.postheader { font-family: sans-serif } tr.address { font-family: sans-serif } body { background: #ffffff; color: black; } </style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">  </td><td class="postheader" valign="center"><a href="../pyqt4ref.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QUndoCommand Class Reference<br /><sup><sup>[<a href="qtgui.html">QtGui</a> module]</sup></sup></h1><p>The QUndoCommand class is the base class of all commands stored on a <a href="qundostack.html">QUndoStack</a>. <a href="#details">More...</a></p> <h3>Methods</h3><ul><li><div class="fn" /><b><a href="qundocommand.html#QUndoCommand">__init__</a></b> (<i>self</i>, QUndoCommand <i>parent</i> = None)</li><li><div class="fn" /><b><a href="qundocommand.html#QUndoCommand-2">__init__</a></b> (<i>self</i>, QString <i>text</i>, QUndoCommand <i>parent</i> = None)</li><li><div class="fn" />QUndoCommand <b><a href="qundocommand.html#child">child</a></b> (<i>self</i>, int <i>index</i>)</li><li><div class="fn" />int <b><a href="qundocommand.html#childCount">childCount</a></b> (<i>self</i>)</li><li><div class="fn" />int <b><a href="qundocommand.html#id">id</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qundocommand.html#mergeWith">mergeWith</a></b> (<i>self</i>, QUndoCommand <i>other</i>)</li><li><div class="fn" /><b><a href="qundocommand.html#redo">redo</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qundocommand.html#setText">setText</a></b> (<i>self</i>, QString <i>text</i>)</li><li><div class="fn" />QString <b><a href="qundocommand.html#text">text</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qundocommand.html#undo">undo</a></b> (<i>self</i>)</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QUndoCommand class is the base class of all commands stored on a <a href="qundostack.html">QUndoStack</a>.</p> <p>For an overview of Qt's Undo Framework, see the <a href="qundo.html">overview document</a>.</p> <p>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 <a href="qundocommand.html#redo">redo</a>() and undo the change with <a href="qundocommand.html#undo">undo</a>(). The implementations for these functions must be provided in a derived class.</p> <pre class="highlightedCode brush: cpp"> 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; }; </pre> <p>A QUndoCommand has an associated <a href="qundocommand.html#text">text</a>(). 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 <a href="qundostack.html#createUndoAction">QUndoStack.createUndoAction</a>() and <a href="qundostack.html#createRedoAction">QUndoStack.createRedoAction</a>().</p> <p>QUndoCommand objects are owned by the stack they were pushed on. <a href="qundostack.html">QUndoStack</a> deletes a command if it has been undone and a new command is pushed. For example:</p> <pre class="highlightedCode brush: cpp"> MyCommand *command1 = new MyCommand(); stack->push(command1); MyCommand *command2 = new MyCommand(); stack->push(command2); stack->undo(); MyCommand *command3 = new MyCommand(); stack->push(command3); <span class="comment">// command2 gets deleted</span> </pre> <p>In effect, when a command is pushed, it becomes the top-most command on the stack.</p> <p>To support command compression, QUndoCommand has an <a href="qundocommand.html#id">id</a>() and the virtual function <a href="qundocommand.html#mergeWith">mergeWith</a>(). These functions are used by <a href="qundostack.html#push">QUndoStack.push</a>().</p> <p>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.</p> <p>The parent in this case is usually an empty command, in that it doesn't provide its own implementation of <a href="qundocommand.html#undo">undo</a>() and <a href="qundocommand.html#redo">redo</a>(). Instead, it uses the base implementations of these functions, which simply call <a href="qundocommand.html#undo">undo</a>() or <a href="qundocommand.html#redo">redo</a>() on all its children. The parent should, however, have a meaningful <a href="qundocommand.html#text">text</a>().</p> <pre class="highlightedCode brush: cpp"> QUndoCommand *insertRed = new QUndoCommand(); <span class="comment">// an empty command</span> insertRed->setText("insert red text"); new InsertText(document, idx, text, insertRed); <span class="comment">// becomes child of insertRed</span> new SetColor(document, idx, text.length(), Qt.red, insertRed); stack.push(insertRed); </pre> <p>Another way to create macros is to use the convenience functions <a href="qundostack.html#beginMacro">QUndoStack.beginMacro</a>() and <a href="qundostack.html#endMacro">QUndoStack.endMacro</a>().</p> <hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QUndoCommand" />QUndoCommand.__init__ (<i>self</i>, <a href="qundocommand.html">QUndoCommand</a> <i>parent</i> = None)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Constructs a <a href="qundocommand.html">QUndoCommand</a> object with parent <i>parent</i>.</p> <p>If <i>parent</i> 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.</p> <p>See also <a href="qundocommand.html#dtor.QUndoCommand">~QUndoCommand</a>().</p> <h3 class="fn"><a name="QUndoCommand-2" />QUndoCommand.__init__ (<i>self</i>, QString <i>text</i>, <a href="qundocommand.html">QUndoCommand</a> <i>parent</i> = None)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Constructs a <a href="qundocommand.html">QUndoCommand</a> object with the given <i>parent</i> and <i>text</i>.</p> <p>If <i>parent</i> 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.</p> <p>See also <a href="qundocommand.html#dtor.QUndoCommand">~QUndoCommand</a>().</p> <h3 class="fn"><a name="child" /><a href="qundocommand.html">QUndoCommand</a> QUndoCommand.child (<i>self</i>, int <i>index</i>)</h3><p>Returns the child command at <i>index</i>.</p> <p>This function was introduced in Qt 4.4.</p> <p>See also <a href="qundocommand.html#childCount">childCount</a>() and <a href="qundostack.html#command">QUndoStack.command</a>().</p> <h3 class="fn"><a name="childCount" />int QUndoCommand.childCount (<i>self</i>)</h3><p>Returns the number of child commands in this command.</p> <p>This function was introduced in Qt 4.4.</p> <p>See also <a href="qundocommand.html#child">child</a>().</p> <h3 class="fn"><a name="id" />int QUndoCommand.id (<i>self</i>)</h3><p>Returns the ID of this command.</p> <p>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.</p> <p>If the command supports compression this function must be overridden in the derived class to return the correct ID. The base implementation returns -1.</p> <p><a href="qundostack.html#push">QUndoStack.push</a>() will only try to merge two commands if they have the same ID, and the ID is not -1.</p> <p>See also <a href="qundocommand.html#mergeWith">mergeWith</a>() and <a href="qundostack.html#push">QUndoStack.push</a>().</p> <h3 class="fn"><a name="mergeWith" />bool QUndoCommand.mergeWith (<i>self</i>, <a href="qundocommand.html">QUndoCommand</a> <i>other</i>)</h3><p>Attempts to merge this command with <i>command</i>. Returns true on success; otherwise returns false.</p> <p>If this function returns true, calling this command's <a href="qundocommand.html#redo">redo</a>() must have the same effect as redoing both this command and <i>command</i>. Similarly, calling this command's <a href="qundocommand.html#undo">undo</a>() must have the same effect as undoing <i>command</i> and this command.</p> <p><a href="qundostack.html">QUndoStack</a> will only try to merge two commands if they have the same id, and the id is not -1.</p> <p>The default implementation returns false.</p> <pre class="highlightedCode brush: cpp"> bool AppendText.mergeWith(const QUndoCommand *other) { if (other->id() != id()) <span class="comment">// make sure other is also an AppendText command</span> return false; m_text += static_cast<const AppendText*>(other)->m_text; return true; } </pre> <p>See also <a href="qundocommand.html#id">id</a>() and <a href="qundostack.html#push">QUndoStack.push</a>().</p> <h3 class="fn"><a name="redo" />QUndoCommand.redo (<i>self</i>)</h3><p>Applies a change to the document. This function must be implemented in the derived class. Calling <a href="qundostack.html#push">QUndoStack.push</a>(), <a href="qundostack.html#undo">QUndoStack.undo</a>() or <a href="qundostack.html#redo">QUndoStack.redo</a>() from this function leads to undefined beahavior.</p> <p>The default implementation calls redo() on all child commands.</p> <p>See also <a href="qundocommand.html#undo">undo</a>().</p> <h3 class="fn"><a name="setText" />QUndoCommand.setText (<i>self</i>, QString <i>text</i>)</h3><p>Sets the command's text to be the <i>text</i> specified.</p> <p>The specified text should be a short user-readable string describing what this command does.</p> <p>See also <a href="qundocommand.html#text">text</a>(), <a href="qundostack.html#createUndoAction">QUndoStack.createUndoAction</a>(), and <a href="qundostack.html#createRedoAction">QUndoStack.createRedoAction</a>().</p> <h3 class="fn"><a name="text" />QString QUndoCommand.text (<i>self</i>)</h3><p>Returns a short text string describing what this command does; for example, "insert text".</p> <p>The text is used when the text properties of the stack's undo and redo actions are updated.</p> <p>See also <a href="qundocommand.html#setText">setText</a>(), <a href="qundostack.html#createUndoAction">QUndoStack.createUndoAction</a>(), and <a href="qundostack.html#createRedoAction">QUndoStack.createRedoAction</a>().</p> <h3 class="fn"><a name="undo" />QUndoCommand.undo (<i>self</i>)</h3><p>Reverts a change to the document. After undo() is called, the state of the document should be the same as before <a href="qundocommand.html#redo">redo</a>() was called. This function must be implemented in the derived class. Calling <a href="qundostack.html#push">QUndoStack.push</a>(), <a href="qundostack.html#undo">QUndoStack.undo</a>() or <a href="qundostack.html#redo">QUndoStack.redo</a>() from this function leads to undefined beahavior.</p> <p>The default implementation calls undo() on all child commands in reverse order.</p> <p>See also <a href="qundocommand.html#redo">redo</a>().</p> <address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt 4.8.3 for X11</td><td align="center" width="50%">Copyright © <a href="http://www.riverbankcomputing.com">Riverbank Computing Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2011</td><td align="right" width="25%">Qt 4.7.1</td></tr></table></div></address></body></html>