开发者

Implementing Undo feature (like Ctrl+Z) in Qt/C++

I am using Qt 4.5 and C++ on Windows XP.

Basically I will be having an UI where the user will enter some data. He can go and modify the values available in the UI. The UI will have basic Qt UI elements like QLineEdit,QTableWidget etc.,

So now, if the user presses Undo button (or Ctrl+Z) the previous value should be retained in the corresponding UI element.

Say, if there is QLineEdit with the text 25. Now the user modifies to 30. Now b开发者_如何学编程y clicking Undo, the older value 25 should be retained.

Like the Undo feature that usually available in many applications. Is there any way to do it?


You could use Qt's undo framework.


The typical way of implementing Undo is to represent each action done by the user, and store them. You also want the ability to compute the inverse of a given action.

So, for an insert into a text buffer, the action would store the text inserted, and the location at which the insert happened. The inverse then becomes a delete, at the same location and with the size of the inserted text.

When the user asks the application to undo, simply look at the most recent stored action, and execute its inverse. If you now instead of deleting the "spent" action remember it, too, you can implement Redo by moving the other way in the history of actions.

Note that this is an abstract and generic explanation; as other answers point out, Qt has a framwork in place for implementing Undo already, so you should of course investigate that, first.


You could use the Command Pattern to realize undo/redo


QLineEdit has a built-in undo/redo support, exported as public slots, check : http://doc.trolltech.com/4.7/qlineedit.html#undo

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜