Implementing Undo - Redo Functionality in Qt?

后端 未结 3 1187
无人及你
无人及你 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:28

    One way of implementing this is by using the 'Command' pattern. Qt provides an undo framework that follows this pattern: http://qt-project.org/doc/qt-4.8/tools-undoframework.html

    To facilitate this, it you may need to make some changes to you program to make it a bit more MVC (model-view-controller). You'd have a data model that represents the contents of your form. The form itself is the view and the controller - any changes to the line edits made by the user would update the data model. The data model would implement changes to it using QUndoCommands which are pushed onto a QUndoStack from the Qt undo framework. When the state of the model changes (due to undo/redo), the UI would respond to the change and be updated to reflect the model's state.

提交回复
热议问题