Using QUndoCommand with QTextEdit



  • When implementing a QUndoStack-based undo functionality, how do you deal with things like QTextEdit? I have two concerns: first, if I attach a slot to textChanged() that creates a QUndoCommand, should I store just the inserted text and insertion point, or should I store the whole before and after text? Second, when the QUndoCommand is called with undo(), it will call setText() on the widget, which will then fire off a textChanged()... of course, I don't want to add a new undo event in this case: how do you all work around this?



  • Answering my own questions here: first, by attaching to textEdited() instead of textChanged(), you are able to ignore any changes to the text made using setText(). Second, since a QLineEdit is a relatively short amount of text I simply stored the old text and the new text: this has the benefit of working regardless of whether the change is an insertion or deletion, letting me use the same Command class. It won't scale well, but for this app that's not a concern.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.