Implementing Undo - Redo Functionality in Qt?

后端 未结 3 1194
无人及你
无人及你 2021-02-08 18:35

My program consists of multiple QLineEdit\'s in my QMainWindow. The user can change the text in any QLineEdit. Currently I can perform

3条回答
  •  我寻月下人不归
    2021-02-08 19:14

    Qt has classes to help with undo, see QUndoStack. Each undoable step should be implemented as a subclass of QUndoCommand.

    What you're trying to achieve is not straightforward because you will need to bypass the internal undo stacks of your QLineEdits. Here's one suggestion: listen for the focusChanged signal from QApplication. If one of your line edits has focus store it's content and connect to the QLineEdit::editingFinished() signal. When this is received put a command on the stack with the old and the new text. The flaw in this approach is that you will not capture intermediate edits in a single QLineEdit. For example if you wanted to store 1) the user selecting text and hitting delete, then 2) typing some new text, as separate undoable steps you might need to start filtering key events and the logic can get quite complex. But that's the general approach.

提交回复
热议问题