[Qt-interest] qt's undo framework + multiple QLineEdits and/or QTextEdits
Will Stokes
wstokes at gmail.com
Tue Jan 5 00:34:15 CET 2010
Consider the following hypothetical application:
-a "document" is made up of 3 fields
-these fields are editable using QLineEdit's, aka, the window you see
is comprised of three line edits, one above the other
My question is, how does one implement undo/redo so that you can do
the following:
-set the three line edits to "A", "B", "C"
-change the 2nd to "Happy"
-change the 3rd to "Sad"
-focus is still on the 3rd, tap Cmd+Z (or Ctrl+Z) to undo the last
change (Sad -> C).
-tap Cmd+Z again to undo the previous change (Happy -> B)
Before you create menu actions the QLineEdit's keyPressEvent will
handle the key strokes on their own. That means after you undo the
last chane (Sad -> C), it is no longer possible to undo the 3rd line
edit further. Since the 3rd line edit still has focus, the 2nd line
edit will never get additional Cmd+Z keystrokes.
I thought I could be clever and approach this like so:
-use the QUndo framework by creating a single QUndoStack for the document
-detect QLineEdit changes and create change commands and push these to
the undo stack
-create an Edit->Undo menu action that calls undo on the QUndoStack.
But this is incredibly problematic, namely:
-when you tap Cmd+Z the Edit menu flashes but the QLineEdit actually
handles the key press. Aka the action is not triggered and thus the
QUndoStack::undo function is never called. The result is we Convert
Sad->C using QLineEdit's internal undo support, and now the undo stack
is out of sync with the real state
-we could try to undo back further by clicking Edit->Undo, but first
we must click through undoing the action we already did.
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?
-Will
More information about the Qt-interest-old
mailing list