How to customize undo/redo for QTextEdit?
-
Hi,
On a QTextEdit, when you type, say, "Hello world" and call the default undo, "Hello world" will be deleted. How do you customize this undo so that when we call undo once, "world" is deleted. Call undo one more time, the space is deleted and call undo one more time, "Hello" is deleted?Thank you very much.
-
Hi,
On a QTextEdit, when you type, say, "Hello world" and call the default undo, "Hello world" will be deleted. How do you customize this undo so that when we call undo once, "world" is deleted. Call undo one more time, the space is deleted and call undo one more time, "Hello" is deleted?Thank you very much.
@ntos
I had a look around and I must warn you that I do not think this is doable. I have looked at bothQTextEdit
and theQTextDocument
which backs it and is where the undo/redo is implemented. None of the methods are virtual, and you cannot access the undo stack provided by theQTextDocument
to replace with your own.That only leaves disabling the inbuilt undo handler and implementing your own, intercepting key presses as required. I think that will be a lot of work/difficult.
-
@JonB . I have looked at some text editor using Qt like focuswriter, featherpad and even Qt creator itself. undoing newly entered text means deleting everything, not very smart I must say. While bluefish or vscode or libreoffice just deletes one word at a time. Thank you very much.
-
-
@ntos
I had a look around and I must warn you that I do not think this is doable. I have looked at bothQTextEdit
and theQTextDocument
which backs it and is where the undo/redo is implemented. None of the methods are virtual, and you cannot access the undo stack provided by theQTextDocument
to replace with your own.That only leaves disabling the inbuilt undo handler and implementing your own, intercepting key presses as required. I think that will be a lot of work/difficult.
@JonB said in How to customize undo/redo for QTextEdit?:
That only leaves disabling the inbuilt undo handler and implementing your own, intercepting key presses as required. I think that will be a lot of work/difficult.
Yes, might be some work, but I think it's doable.
Maybe not with the existingQTextEdit
but with a custom implementation which uses your ownQUndoStack
.Something like:
Detect new word (e.g. space key pressed), split text, push last onto undoStack, repeat.
Then you can revert the last inserted word... I think so... obviously not tested :) -
@JonB said in How to customize undo/redo for QTextEdit?:
That only leaves disabling the inbuilt undo handler and implementing your own, intercepting key presses as required. I think that will be a lot of work/difficult.
Yes, might be some work, but I think it's doable.
Maybe not with the existingQTextEdit
but with a custom implementation which uses your ownQUndoStack
.Something like:
Detect new word (e.g. space key pressed), split text, push last onto undoStack, repeat.
Then you can revert the last inserted word... I think so... obviously not tested :)@Pl45m4 said in How to customize undo/redo for QTextEdit?:
Detect new word (e.g. space key pressed),
This sort of thing will be way harder to do and be correct/consistent than you estimate. Remember the user can be editing all sorts of stuff, not just appending new text at the end. Just for example, inserting a character in the middle of an existing word is an undoable action. Which might or might not combine with typing a second character. In a short period of time. And not if there is an intervening extra character between the two new ones.... You have the whole of the existing undo logic for
QTextEdit
to implement before you even get as far as adding your new rules....