Undo/Redo in PyQt

David Boddie david at boddie.org.uk
Wed Jan 13 18:13:57 EST 2010


On Wednesday 13 January 2010 02:20, Zabin wrote:

> I am trying to implement the undo and redo facility in pyqt. I have
> gone through some sites and was wondering whether iyou always need to
> create subclasses and their definitions for the undo/redo action. My
> program currently has a single window in which the user enters
> information which is used to update a text file as soon as the user
> leaves the field- so i essentially need to undo the text in that field
> as well as the file. I am puzzled as to how to approach this. Any help
> will be much appreciated!

>From memory and a quick glance at the documentation for QUndoCommand, I'd
say the answer to your question is yes, you do need to subclass QUndoCommand
for each kind of command you want to make undoable.

You need to derive a subclass because you need to implement undo() and
redo() methods that perform the work of adding the text to the file (redo)
and removing it (undo). You might optionally write text back to the field
when performing an undo command, as well.

I've put some example code on this page:

  http://www.diotavelli.net/PyQtWiki/Undo_and_redo_with_line_edits

It's not ideal because the commands are invoked when the window itself
loses focus, but perhaps you can use it as a starting point.

David



More information about the Python-list mailing list