Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to customize undo/redo for QTextEdit?
Forum Updated to NodeBB v4.3 + New Features

How to customize undo/redo for QTextEdit?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 617 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    ntos
    wrote on last edited by
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • N ntos

      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.

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @ntos
      I had a look around and I must warn you that I do not think this is doable. I have looked at both QTextEdit and the QTextDocument 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 the QTextDocument 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.

      Pl45m4P 1 Reply Last reply
      0
      • N Offline
        N Offline
        ntos
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • N ntos has marked this topic as solved on
        • JonBJ JonB

          @ntos
          I had a look around and I must warn you that I do not think this is doable. I have looked at both QTextEdit and the QTextDocument 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 the QTextDocument 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.

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #4

          @ntos

          @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 existing QTextEdit but with a custom implementation which uses your own QUndoStack.

          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 :)

          • https://doc.qt.io/qt-6/qtwidgets-tools-undoframework-example.html

          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          JonBJ 1 Reply Last reply
          0
          • Pl45m4P Pl45m4

            @ntos

            @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 existing QTextEdit but with a custom implementation which uses your own QUndoStack.

            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 :)

            • https://doc.qt.io/qt-6/qtwidgets-tools-undoframework-example.html
            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #5

            @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....

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved