<?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="index.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" />QString <b><a href="qundocommand.html#actionText">actionText</a></b> (<i>self</i>)</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="cpp"> <span class="keyword">class</span> AppendText : <span class="keyword">public</span> <span class="type">QUndoCommand</span> { <span class="keyword">public</span>: AppendText(<span class="type"><a href="qstring.html">QString</a></span> <span class="operator">*</span>doc<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>text) : m_document(doc)<span class="operator">,</span> m_text(text) { setText(<span class="string">"append text"</span>); } <span class="keyword">virtual</span> <span class="type">void</span> undo() { m_document<span class="operator">-</span><span class="operator">></span>chop(m_text<span class="operator">.</span>length()); } <span class="keyword">virtual</span> <span class="type">void</span> redo() { m_document<span class="operator">-</span><span class="operator">></span>append(m_text); } <span class="keyword">private</span>: <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">*</span>m_document; <span class="type"><a href="qstring.html">QString</a></span> 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="cpp"> MyCommand <span class="operator">*</span>command1 <span class="operator">=</span> <span class="keyword">new</span> MyCommand(); stack<span class="operator">-</span><span class="operator">></span>push(command1); MyCommand <span class="operator">*</span>command2 <span class="operator">=</span> <span class="keyword">new</span> MyCommand(); stack<span class="operator">-</span><span class="operator">></span>push(command2); stack<span class="operator">-</span><span class="operator">></span><a href="qundocommand.html#undo">undo</a>(); MyCommand <span class="operator">*</span>command3 <span class="operator">=</span> <span class="keyword">new</span> MyCommand(); stack<span class="operator">-</span><span class="operator">></span>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="cpp"> <span class="type">QUndoCommand</span> <span class="operator">*</span>insertRed <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QUndoCommand</span>(); <span class="comment">// an empty command</span> insertRed<span class="operator">-</span><span class="operator">></span><a href="qundocommand.html#setText">setText</a>(<span class="string">"insert red text"</span>); <span class="keyword">new</span> InsertText(document<span class="operator">,</span> idx<span class="operator">,</span> text<span class="operator">,</span> insertRed); <span class="comment">// becomes child of insertRed</span> <span class="keyword">new</span> SetColor(document<span class="operator">,</span> idx<span class="operator">,</span> text<span class="operator">.</span>length()<span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">.</span>red<span class="operator">,</span> insertRed); stack<span class="operator">.</span>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><b>See also</b> <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><b>See also</b> <a href="qundocommand.html#dtor.QUndoCommand">~QUndoCommand</a>().</p> <h3 class="fn"><a name="actionText" />QString QUndoCommand.actionText (<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>This function was introduced in Qt 4.8.</p> <p><b>See also</b> <a href="qundocommand.html#text">text</a>(), <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="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><b>See also</b> <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><b>See also</b> <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><b>See also</b> <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="cpp"> <span class="type">bool</span> AppendText<span class="operator">.</span>mergeWith(<span class="keyword">const</span> <span class="type"><a href="qundocommand.html">QUndoCommand</a></span> <span class="operator">*</span>other) { <span class="keyword">if</span> (other<span class="operator">-</span><span class="operator">></span>id() <span class="operator">!</span><span class="operator">=</span> id()) <span class="comment">// make sure other is also an AppendText command</span> <span class="keyword">return</span> <span class="keyword">false</span>; m_text <span class="operator">+</span><span class="operator">=</span> <span class="keyword">static_cast</span><span class="operator"><</span><span class="keyword">const</span> AppendText<span class="operator">*</span><span class="operator">></span>(other)<span class="operator">-</span><span class="operator">></span>m_text; <span class="keyword">return</span> <span class="keyword">true</span>; } </pre> <p><b>See also</b> <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><b>See also</b> <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>If you need to have two different strings for <a href="qundocommand.html#text">text</a>() and <a href="qundocommand.html#actionText">actionText</a>(), 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 <a href="qundocommand.html#actionText">actionText</a>() are available since Qt 4.8.</p> <p><b>See also</b> <a href="qundocommand.html#text">text</a>(), <a href="qundocommand.html#actionText">actionText</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 for names of items in <a href="qundoview.html">QUndoView</a>.</p> <p><b>See also</b> <a href="qundocommand.html#actionText">actionText</a>(), <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><b>See also</b> <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.10.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> 2012</td><td align="right" width="25%">Qt 4.8.5</td></tr></table></div></address></body></html>