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 can I show the QTextDocument Undo stack in QUndoView?
Forum Updated to NodeBB v4.3 + New Features

How can I show the QTextDocument Undo stack in QUndoView?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 3.8k 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.
  • D Offline
    D Offline
    dcortesi
    wrote on last edited by
    #1

    I would like to display the contents of the undo stack for an editor (specifically a QPlainTextEdit) using QUndoView. However, it seems that QTextDocument does not provide a reference to its undo stack. You can query the number of steps in the stack(s), clear them, or request a single Undo. But I would like to pass the stack reference to a view for display.

    I am using PyQt4 and Qt4.8 (I checked the 5.1 documentation also).
    @editor = QPlainTextEdit()
    undoview = QUndoView(editor.document().undoStack()) # this is not possible
    @

    Is there any way to do this that I have overlooked? Or would it be useful to request it as a feature?

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      since the private class of the QTextDocument holds the undo/redo stacks and they are not available via API you have no chance to access them like you want to do.
      The only way would be to implement your own undo/redo stack and create and manage the commands yourself. Which should be very straigth forward for a QPlainTextEdit widget.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dcortesi
        wrote on last edited by
        #3

        Wait, what? Could you add a few words on how one might do that? All I can imagine is, tell the editor's QTextDocument that undo is not available, then, intercept every keystroke, interpret it, make a QUndoCommand to describe it, and stack it, and then pass the keystroke on to the editor to execute... When the keystroke is ^Z pop the stack and... I'm not sure what next...

        Is that what you are suggesting?

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          basically yes. But i don't know whats the performance drawback ... depending on your implementation.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • B Offline
            B Offline
            bootchk
            wrote on last edited by
            #5

            About your proposed solution: your subclassed QUndoCommand, when stacked, will execute its redo() method. Its redo() method is what should pass a keystroke to the editor.

            On Ctl-Z you just tell the QUndoStack to undo(). It manages its stack itself, you don't "unstack" it. The undo() method of your subclassed QUndoCommand should rip the keystroke out of the document.

            I have a similar need. I have a widget that is a text editor. Since its document has its own undo stack, I planned to let the user undo changes to that widget only when that widget is focused. When not focused, Ctl-Z undoes another operation on the main undoStack. Its a user interface design issue.

            (But when I call QTextDocument.undo(), it seems to have no effect? I'm doing something wrong I suppose.)

            1 Reply Last reply
            0
            • B Offline
              B Offline
              bootchk
              wrote on last edited by
              #6

              My app is programmatically inserting keystrokes into a QGraphicsTextItem by calling someCursor.insertText(). (I am not letting Qt handle events in the graphics item.) I find that it takes three calls to undo() to undo this. And it only undoes the last keystroke inserted (I had hoped it would merge the inserted keystroke operations and undo them all at once.)

              I wouldn't underestimate the work needed to mirror the undo stack built into (and hidden in) QTextDocument.

              1 Reply Last reply
              0

              • Login

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