Cool_Coder
Cool_Coder

Reputation: 5073

Unable to understand Undo Redo Framework in 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.

Upvotes: 6

Views: 3818

Answers (1)

Chen
Chen

Reputation: 1672

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.

Upvotes: 9

Related Questions