Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTextBlockUserData and Undo stack



  • Hi all,

    I would like to ask if anyone of you know how to implement undo feature for QTextDocument to store QTextBlockUserData

    On Qt doc page for QTextBlock::setUserData (QTextBlockUserData * data) function is written:

    "The user data object is not stored in the undo history, so it will not be available after undoing the deletion of a text block"

    So how to be able to restore user data for deleted text?



  • A bit late for this reply perhaps but this is actually fairly simple to do using the QTextFormat::UserProperty (0x100000) of the associated QTextBlockFormat for the QTextBlock Instead of getting or setting userData functions. The QTextBlockFormat properties are preserved during undo and redo operations. Instead of storing a data pointer you save the data as a QVariant in the format properties.

    Note, you can also do this for any of the formats that are derived from QTextFormat (i.e., QTextBlockFormat, QTextCharFormat, QTextFrameFormat, and QTextListFormat). In my case I have a simple set of indexes that reference entries to a QHash stored in a structure for my format userData and I have no trouble with undo/redo functions. I also store userData in both block and frame formats successfully using a set of helper functions I developed for that purpose but if the userData is simple enough then setting or getting the data directly is relatively straightforward.


Log in to reply