[PyQt] Undo/Redo and operator delete
Paul Du Bois
dubois at doublefine.com
Fri Feb 4 20:09:57 GMT 2011
http://doc.qt.nokia.com/qq/qq25-undo.html has this snip of example code
that demonstrates (among other things) how to "back out" commands that
fail or otherwise do nothing so they don't clutter up the undo stack:
bool UndoRedoProxy::setData( ... ){
if (!index.isValid() || index.data(role) == value
|| value.isNull()) return false;
SetDataCommand *cmd = new SetDataCommand(index, value,
role, m_helper);
cmd->setText(setDataText(index, value, role));
m_stack->push(cmd);
if(!m_cachedResult) delete m_stack->command(0); // <---- the
line of interest
return m_cachedResult;
}
Two questions -- how is the delete operator exposed to PyQt? And
perhaps more important, is this really correct? I've looked over the
source to qundostack and from what I can tell, there is no smart pointer
magic going on and m_stack->d->command_list will be left with a dangling
pointer after the delete.
Yet, the example does appear in a relatively recent Nokia publication.
It really would be handy to be able to pop commands off the stack, but
the QUndoStack API seems too narrow to allow it. The best I can think
of is not to put it on in the first place, something like:
cmd = SomeUndoableCommand(...)
cmd.redo()
if not cmd.failed:
cmd.hack_ignore_next_redo = True
undo_stack.push(cmd)
p
More information about the PyQt
mailing list