Making changes to a QTextEdit without adding an undo command to the undo stack

pyside, qt, qtextedit

Solution

You have to group this with previous modification. It is simple you have to surround code which does this modification with: `beginEditBlock` and `endEditBlock`. See documentation.

text_cursor = myTextEdit.textCursor()
text_cursor.beginEditBlock()
text_cursor.setCharFormat(someOtherCharFormat) # some previous modification
text_cursor.setBlockCharFormat(someNewCharFormat)
text_cursor.endEditBlock()

this way you will make a single commit for undo stack for any complex modification.

Problem

I'm looking for a way to change the `QTextCharFormat` of a `QTextEdit`'s `QTextBlock` without triggering the addition of an undo command. Let me explain: The `QTextCharFormat` of a `QTextBlock` can be easily changed by using the `QTextCursor::setBlockCharFormat()` method. Assuming we have a `QTextEdit` called `myTextEdit` whose visible cursor is within the text block we want to change, we can change the textblock's `QTextCharFormat` like so: ``` text_cursor = myTextEdit.textCursor() text_cursor.setBlockCharFormat(someNewCharFormat) ``` The above code works fine, but it will also add an undo command to the `myTextEdit` undo stack. For my own purposes, I would like to be able to change the `QTextCharFormat` of a `QTextBlock` without adding an undo command to the `QTextEdit`'s undo stack. I considered temporarily disabling the undo/redo system with the `QTextDocument::setUndoRedoEnabled()` method, but that method also clears the undo stack, which I don't want to do. I've also looked for other ways to change how the undo/redo system behaves, but I haven't found a way to get it to temporarily ignore changes. I simply want to make a change to a `QTextEdit` without the undo/redo system registering the change at all. Any tips or suggestions are appreciated. Thanks!

Original source