Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to understand Undo Redo Framework in Qt

Tags:

c++

qt

I am learning to use Qt for my application development & I am pretty successfull in developing my Application. Now I want to implement Undo Redo Functionality for my Application. The doc for this topic has little information. I have even tried understanding from the 2 examples in the SDK. But I am having a tough time understanding how it works. Can somebody please take the trouble of explaining me how to implement it? There are various state's in my application for which I want to provide this functionality. So can the explaination be from the general point of view? If there are already articles on the internet explaining the same then please notify me about them. That would be very helpful. Thank You.

like image 771
Cool_Coder Avatar asked Mar 03 '13 13:03

Cool_Coder


People also ask

How do you undo in Qt?

Undo and redo are available through the edit menu. The user can also select a command from the undo view. We use the graphics view framework to implement the diagram. We only treat the related code briefly as the framework has examples of its own (e.g., the Diagram Scene Example).

How does undo/redo work?

To undo an action, press Ctrl + Z. To redo an undone action, press Ctrl + Y. The Undo and Redo features let you remove or repeat single or multiple typing actions, but all actions must be undone or redone in the order you did or undid them – you can't skip actions.

How do you implement undo/redo stack?

If “UNDO” string is encountered, pop the top element from Undo stack and push it to Redo stack. If “REDO” string is encountered, pop the top element of Redo stack and push it into the Undo stack. If “READ” string is encountered, print all the elements of the Undo stack in reverse order.

How do you implement undo/redo in Python?

Most Python commands which affect the underlying state of an object create an undo/redo action which can then be undone or redone through Python or through the Edit > Undo and Redo menu items.


1 Answers

There are 2 core classes: QUndoCommand and QUndoStack;

  1. QUndoCommand is the base class of your command class. You have to implement undo() and redo() yourself.
  2. QUndoStack is basically a container of QUndoCommand objects, with extra methods like creating QAction, query undo/redo text of current QUndoCommand(Simple functionalities which you can implemented yourself easily)

What you need to do is:

  1. Implement your commands. You need to decide how to implement redo/undo yourself according to your need. class AppendText is a good example: http://qt-project.org/doc/qt-5.0/qtwidgets/qundocommand.html
  2. Keep a QUndoStack instance for each document(Or one instance if there is only one document in the application).
  3. Say you have an "AppendText" command class, and an "Append" button in the UI. If "Append" button is clicked, you need to create an AppendText command instance, and call QUndoStack::push(appendCmd). QUndoStack::push() will call AppendText::redo() first, and then put it into the container for undo.

That's it.

like image 139
Chen Avatar answered Oct 18 '22 18:10

Chen