Making changes to a QTextEdit without adding an undo command to the undo stack
I'm looking for a way to change the
QTextCharFormat of a
QTextBlock without triggering the addition of an undo command. Let me explain:
QTextCharFormat of a
QTextBlock can be easily changed by using the
QTextCursor::setBlockCharFormat() method. Assuming we have a
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!
You have to group this with previous modification. It is simple you have to surround code which does this modification with:
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.
qt, qt - Making changes to a QTextEdit without adding an undo command to the undo the above code works fine, add undo command mytextedit undo stack. own Thanks for contributing an answer to SharePoint Stack Exchange! Please be sure to answer the question. Provide details and share your research! But avoid … Asking for help, clarification, or responding to other answers. Making statements based on opinion; back them up with references or personal experience.
If you feel QSyntaxHighlighter is not what you want, you can use QTextLayout. It is low level api and its
setAdditionalFormats func doesn't make any undo stack.
range1 = QTextLayout.FormatRange() range1.start = 0 range1.length = 10 range1.format = QTextCharFormat() # additional ranges here... textBlock.layout().setAdditionalFormats([range1, ...])
This is also used in the inside of QSyntaxHighlighter.
QTextEdit undo/redo, The undo/redo works for the current document's changes, but after a document switch the text edit says no undo avaiable. Hence my and had to create my undo commands, only to do something like textEdit->undo() in it. I realise that this thread is fairly old, but i have ran into a similar problem using QT 4.5 on Windows. If you want to set a selection in QTextEdit just create one on a QTextCursor object and then make that cursor the visible cursor using setTextCursor (). The selection can be copied to the clipboard with copy (), or cut to the clipboard with cut (). The entire text can be selected using selectAll ().
joinPreviousBlock() should do the trick:
cursor = self.textCursor() cursor.joinPreviousEditBlock() cursor.setPosition(start, QTextCursor.MoveAnchor) cursor.setPosition(end, QTextCursor.KeepAnchor) cursor.setCharFormat(fmt) cursor.endEditBlock()
Is it possible to batch text changes for undo/redo?, I am developing an editor based on QPlainTextEdit. however each replacement is a single command for undo purposes. I would like to be able to batch all these changes as a single Undo step the UndoRedo example code, they create their own UndoStack object. No other class seems to have this. A potentially helpful addition to the UndoRedoStack class is a set of methods that will add the command to the internal undo stack without executing the command. That will be needed when implementing a stack for, say, a RichTextBox. The RichTextBox executes its own commands. For example, suppose you type a letter.
How can I show the QTextDocument Undo stack in QUndoView?, of the undo stack for an editor (specifically a QPlainTextEdit) using the undo/redo stacks and they are not available via API you have no undo/redo stack and create and manage the commands yourself. Since its document has its own undo stack, I planned to let the user undo changes to that widget To maintain the states of an application in all the three approaches we uses two stacks. One stack contains the states for undo operation. The second stack contains the states for redo operation. Undo operation pops the undo stack to get the previous state and sets the previous state to the application.
Undo/redo works in some text areas but not others · Issue #34 , All other text areas seem to have undo/redo working. The undo/redo commands don't work in main editor, notes/references, and World And no CTRLZ. See QTBUG-55618: QTextEdit undo fails after using QTextCursor (even with Following is a git diff of the two line changes to recreate the test I did: A user changes the state of the program, and then wishes to reverse that state to the previous - be it to remove a mistake, make a simple comparison, or follow the progression of their actions. Many java swing components already provide basic undo/redo features, in particular the JTextComponent classes.
QPlainTextEdit, QTextCursor object and then make that cursor the visible cursor using PySide. textChanged() signal if the text changes and it also provides a isModified() function which will return true Returns the reference of the anchor at position pos , or an empty string if no Note that the undo/redo history is cleared by this function. To undo recent changes, from normal mode use the undo command: u: undo last change (can be repeated to undo preceding commands) Ctrl-r: Redo changes which were undone (undo the undos). Compare to . to repeat a previous change, at the current cursor position.
- I think you may be out of luck, see this qt-interest thread (which is a bit old, but it summarizes the issues quite well).
- Hi Marek. Thanks for the suggestion! Unfortunately, this solution doesn't accomplish what I need. I really need there to be no undo command added at all. The reason I need this is that I am trying to expand the undo/redo system to track and change attributes of the overall program that it doesn't track by default. This means that, when an action is undone, I am executing additional code that could be thought of as part of the undo action. In my case, the setBlockCharFormat() method would be run as part of an undo, and therefore, should not add an undo command to the stack.
- so try
undothen inside edit block
redoand than your change.