Remove first QUndoCommand from QUndoStack
My program uses the Qt Undo Framework. The individual commands may use a lot of memory, which can lead to insufficient memory. This could be handled by setting undoLimit, but it requires setting the limit prior to using the stack (i.e, when empty). I would like to detect, when the commands start using too much memory, and delete the first (few) commands (the oldest ones) to limit the memory usage of the undo stack.
Looking into the source code, it seems, like the QUndoStackPrivate class could handle this (setting the limit when the stack is not empty), however it is hidden in Qt and QUndoStack checks the emptiness of the stack explicitly, and allows setting the limit only if it is empty.
Is there a way to delete the first item of a undo stack (e.g. by reparenting the commands to a new stack)? If not, is there any reason, the emptiness of the stack is required to set the stack limit? It would be enough to check whether the current command is newer, than the new limit (just like QUndoStackPrivate does).
Out of curiosity, what makes your command eat so much memory ? If you need "infinite size", you could maybe use a temporary on disk cache to store the data you need.
My application can handle and edit images, although it is not a image editor. Editing an image is one step in the undo stack, so I do not want to store each individual editing steps, thus I chose to store the whole image in the command. Furthermore it can handle several images simultaneously, which may come from DSLR cameras, so the memory requirement can easily become large.
(Probably I could and will optimize the commands to use less memory, but my original question is somewhat independent of this very application.)
Using disk cache could be a solution, indeed. However I do not need infinite stack. It would be fine to loose old commands from the stack if they use too much memory. But as the memory requirement of the commands cannot be predetermined (as it depends on the user actions), it would be nice if I could control the stack size dynamically based on the memory consumption of the commands.
As I said, it seems that the guy, who coded the underlying QUndoStackPrivate class seems to have prepared for such situations, but for some reason, the other guy, who coded the QUndoStack class seems to have decided to hide that possibility from the developers and introduced an unnecessarily rigorous condition to allow changing the stack size.