[PyQt] PyQt's Undo/Redo framework - A Very Short Intro

Giovanni Bajo rasky at develer.com
Thu Apr 24 21:16:43 BST 2008


On 4/24/2008 12:30 PM, Mark Summerfield wrote:
> Hi,
> 
> I've written a very short introductory article on using PyQt's Undo/Redo
> framework. It is available here:
> 
> http://www.informit.com/articles/article.aspx?p=1187104
> 
> (The indentation of the last line of the delete() method is wrong---I've
> told InformIT so they may have fixed it by now, but if not you'll spot
> it anyway.)
> 
> The article has been heavily edited for style so it doesn't read like my
> usual writing, but the content is fine:-)

Hi Mark,

nice article, thanks!

A couple of caveats about lifetimes issues. In page 3:

==============================================================
     self.undoStack = QUndoStack(self)

Add this line to create an undo stack. Although it’s an instance 
variable, you still must give it a parent (the dialog box), so that PyQt 
is able to clean it up at the right time when the dialog box is destroyed.
==============================================================

Is this really true? Without a parent, the undo stack would be garbage 
collected whenever the Python instance is garbage collected, which is 
probably right at the moment the dialog is destroyed (or sometimes 
after, but does it really mind?).

Later:
==============================================================
     def redo(self):
         item = self.listWidget.takeItem(self.row)
         del item
==============================================================

IMO it's a little confusing because the "del item" isn't really doing 
anything more than it would be done one opcode later when the method 
exits and its locals are destroyed. In fact, calling takeItem() without 
binding its return value to a name achieves the same effect and it is 
clearer.

Thanks again for a nice article!
-- 
Giovanni Bajo
Develer S.r.l.
http://www.develer.com




More information about the PyQt mailing list