Implementing the Undo - Redo Function in Qt?

My program consists of several QLineEdit in my QMainWindow . The user can change the text in any QLineEdit . I can currently cancel the default re-implementation for each QLineEdit when the corresponding QLineEdit selected. However, I want to add functionality so that when you press the cancel buttons * redo *, the appropriately edited QLineEdit will cancel / redo. i.e. L1, L2, etc. are my QLineEdit . Then the user performs the following actions: 1. L2-> text added ... 5. L5-> text added ... 9. L3-> text deleted 10. L5-> undo (by choosing L5 and then "ctrl + z" )

Now, when the user presses the Undo / Redo button as follows: 1. Undo β†’ Undo the undo in step 10 2. Undo β†’ Undo the text deleted in step 9 3. Repeat β†’ Undo the text deleted in step 9 4. Undo β†’ You must cancel the operation in step 8

Currently, I can vaguely think about the mechanism for creating History, but it will be quite tiring. So I would like to know if Qt provides any built-in functions for this? Thanks.

+7
source share
3 answers

Qt provides a Undo / Redo framework.

Basically you execute the command received from QUndoCommand for each of the actions you want to undo / reinstall, and then click them on QUndoStack . Later, the commands can be undone and redone by calling the corresponding slots on QUndoStack .

Please note that when a command is pressed on the stack, it is automatically redone, which means it is executed for the first time. Sometimes this is not what you want, as the command may already be executed - you will need to consider this when implementing your commands.

+9
source

One way to implement this is to use the Command pattern. Qt provides a cancellation structure that follows this pattern: http://qt-project.org/doc/qt-4.8/tools-undoframework.html

To facilitate this, you may need to make some changes to your program to make it a bit more MVC (model-view-controller). You will have a data model representing the contents of your form. The form itself is a representation and a controller - any changes in the editing of strings made by the user will update the data model. The data model will implement changes to it using QUndoCommands, which are pushed to QUndoStack from the Qt undo framework. When the state of the model changes (due to undo / redo), the user interface will respond to the change and update to reflect the state of the model.

+3
source

Qt has classes to help undo, see QUndoStack . Each invalid step must be implemented as a subclass of QUndoCommand .

What you are trying to achieve is not simple, because you will need to bypass the internal desks of canceling your QLineEdit s. Here's one suggestion: listen for the focusChanged signal from QApplication . If one of your lines has focus, save its contents and connect to the QLineEdit::editingFinished() signal. When this is received, put the command on the stack with old and new text. The disadvantage of this approach is that you will not capture intermediate changes in one QLineEdit . For example, if you want to save 1) the user by selecting the text and deleting the deletion, then 2) entering the text as separate canceled steps, you may need to start filtering key events, and the logic can become quite complicated. But a general approach.

+1
source

All Articles