[Qt-interest] qt's undo framework + multiple QLineEdits and/or QTextEdits

Kaleb Pederson kaleb.pederson at gmail.com
Tue Jan 5 17:51:03 CET 2010


On Mon, Jan 4, 2010 at 3:34 PM, Will Stokes <wstokes at gmail.com> wrote:
[...snip...]

> I suppose my life would be infinitely easier if I could just disable
> internal undo/redo support for selective QLineEdit and QTextEdit's in
> my application and instead detect chnages to those widgets and
> maintain a single QUndoStack for the document as a whole instead.
>
> Has anyone else tried to do this and if so what approach worked best for you?

Here's what I did:

1) Setup a manager class that monitors widgets that have any internal
support for undo/redo (e.g. those that emit undoAvailable and
redoAvailable, or custom widgets that support undo/redo)
2) Register the widgets of interest (i.e. the QLineEdits and
QTextEdits) with my manager class

Then, the manager class interacts with the QUndoStack in the following way:

a) When one of the monitored line edit is changed, it emits an
undoAvailable signal
b) The undoAvailable signal is picked up by the manager class
c) The manager class creates a QUndoCommand that acts as a proxy for
the widget that emited undoAvailable in (a).

Thus, when undo/redo is needed, the undostack is the definitive
source. The user can hit undo at any point in time.  When he does:

i) The application calls undo on the undo stack
ii) The undo stack either:
  ii.a) calls the undo command, which then calls lineEdit->undo(), or
  ii.b) calls the undo command, which then handles undo/redo itself

One note, I'm not sure that your "focus is still on the 3rd, tap
Cmd+Z" will actually generate an undoAvailable(bool) signal, but a
little bit of creativity on the signals monitored by the manager class
make it possible if it's not there by default.

I hope that helps.

--
Kaleb Pederson

Blog - http://kalebpederson.com
Twitter - http://twitter.com/kalebpederson



More information about the Qt-interest-old mailing list