QTextDocument undo/redo stack - would it be better if we could access it?



  • Re: How can I show the QTextDocument Undo stack in QUndoView? - this question was previously asked several years ago. A few related questions have also been asked.

    The Undo stack of QTextDocument is more or less inaccessible and internally isn't compatible with QUndoView anyway. This is a limitation and inadequacy of QTextDocument and to work around you are likely to be forced to re-invent the wheel and manage your own Undo/Redo stack.

    Imagine an application which provides a text view of a configuration file (perhaps something like an INI file or JSON text) but also provides an editable tree view of the same underlying document. The user can edit in either view and to complicate matters the QPlainText edit also uses syntax highlighting (this can cause additional edits to be pushed to the undo stack when you least expect!). Now in the tree view imagine that the delegate creates a QDoubleSpinBox editor for a particular item. The user adjusts this particular configuration value using this editor. As changes in the tree view are immediately reflected in the text editor view any changes made are internally performed by editing the QTextDocument programatically e.g inserting or deleting but in order to allow the user to undo we make use of QTextDocument's undo/redo capabilities. Unfortunately when the user holds down the spin box arrow we are going to get hundreds of changes pushed to the text document. We know that we would really rather merge such changes and would if we could!!! So what do we do?

    I have such an application and currently am contemplating this very question and it does seem that the solution is to throw away QTextDocument undo/redo support and start over from scratch. Such a shame!

    Can anyone suggest a better way e.g a way to tell the QTextDocument to take the last two items on the undo stack and merge them into a single command? Using a QUndoView would be nice (and great for debugging) but really all I need is a way to merge the last two commands.

    PS. The QUndoGroup/QUndoStack/QUndoView/QUndoCommand framework looks very nice and particularly relevant if you have an application that simultaneously can allow the user to edit multiple documents. My application is in this category! Because QTextDocument does not currently support this framework (what does?) my undo/redo is limited to per document and not global scope - which is also a shame.

    The bottom line is it would be nice if we had something like QTextDocument that actually gave us more control of the undo/redo stack and ideally which was compatible with the QUndoXXX framework. Does anyone know of any plans to do this or of any suitable 3rd party code that can be used now?


  • Moderators

    @navsystems said in QTextDocument undo/redo stack - would it be better if we could access it?:

    Can anyone suggest a better way e.g a way to tell the QTextDocument to take the last two items on the undo stack and merge them into a single command? Using a QUndoView would be nice (and great for debugging) but really all I need is a way to merge the last two commands.

    This is already supported using QUndoCommand::mergeWith()



  • @raven-worx said in QTextDocument undo/redo stack - would it be better if we could access it?:

    @navsystems said in QTextDocument undo/redo stack - would it be better if we could access it?:

    Can anyone suggest a better way e.g a way to tell the QTextDocument to take the last two items on the undo stack and merge them into a single command? Using a QUndoView would be nice (and great for debugging) but really all I need is a way to merge the last two commands.

    This is already supported using QUndoCommand::mergeWith()

    Except (unless the QT 5.10 documentation isn't up to date (I'm still using QT5.9)) then its fine that QUndoCommand supports this BUT QTextDocument does not expose its undo stack and even if it internally used QUndoCommand we can't get at it!


  • Moderators

    @navsystems
    yes, and that is what i already explained in the thread you are referring to


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.