<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Store QTextBlock user data without deleting it after undo of  TextEdit Qml]]></title><description><![CDATA[<p dir="auto">Am tryng to implement folding and folding of QtextBlocks in qml without tracking them in the undo and redo..<br />
I tried using QTextBlock::setVisible but when i set it to false..the app crashes, even when i add  QTextDocument::markContentsDirty.</p>
<p dir="auto">I tried using QTextBlock::setUserData with the QTextBlockUserData object, and it works!, but if the user undoes, the object is deleted leading to loss of data since the block text is stored in the object. And Qt documentation warns us not to do any internal manipulations of the QQuickTextDocument object returned by the TextEdit::textDocument or else will crash!</p>
<pre><code>void DocumentHandler::toggleBlock(int blockPosition) {
  if (textDocument()) {
    auto textBlock = textDocument()-&gt;findBlockByNumber(blockPosition);
    if (textBlock.isValid()) {

      // this is nullptr whenever the the text block is undone!!
      auto userBlockData =
          dynamic_cast&lt;GwTextBlockData *&gt;(textBlock.userData());

       if (!userBlockData) {
        // hence forcing this object to be recreated the second time....this is ugly!
        userBlockData = new GwTextBlockData;
        textBlock.setUserData(userBlockData);
      } 

      if (userBlockData-&gt;isVisible()) {
        foldBlock(textBlock);
      } else {
        unFoldBlock(textBlock);
      }

      emit updateBlockHightligterPosition();
    }
  }
}

void DocumentHandler::foldBlock(QTextBlock &amp;textBlock) {

  auto userBlockData = dynamic_cast&lt;GwTextBlockData *&gt;(textBlock.userData());
  if (!userBlockData) {
    qDebug() &lt;&lt; "Failed to get block data ..";
    return;
  }

  // store block text before folding
  userBlockData-&gt;setBlockText(textBlock.text());

  QTextCursor blockCursor(textBlock);

  // select first line of the block

  int currentCursorPosition = blockCursor.position();

  blockCursor.select(QTextCursor::BlockUnderCursor);

  auto blocKtext = textBlock.text();
  auto firstLine =
      blocKtext.mid(0, 50); // this is the text that will remain visible to the
                            // user when th block is folded

  auto remaining = blocKtext.remove(firstLine); // this is the rest of the block
                                                // text to be hidden

  int start = currentCursorPosition + firstLine.length();
  int end = currentCursorPosition + textBlock.text().length();

  blockCursor.setPosition(start, QTextCursor::MoveAnchor);
  blockCursor.setPosition(end, QTextCursor::KeepAnchor);

  // prevent registering this operation to the undo and redo stack
  blockCursor.joinPreviousEditBlock();
  // remove the remaining text to give an illusion of folding;
  blockCursor.removeSelectedText();
  blockCursor.endEditBlock();

  // store the block state in the text block object
  userBlockData-&gt;setFirstLineText(firstLine);
  userBlockData-&gt;setRemainingText(remaining);
  userBlockData-&gt;setIsVisible(false);
}


void DocumentHandler::unFoldBlock(QTextBlock &amp;textBlock) {

  QTextCursor blockCursor(textBlock);

  auto userBlockData = dynamic_cast&lt;GwTextBlockData *&gt;(textBlock.userData());

  if (!userBlockData) {
    qDebug() &lt;&lt; "Failed to get block data ..";
    return;
  }

  // prevent registering this operation to the undo and redo stack
  blockCursor.joinPreviousEditBlock();
  // revealing block
  blockCursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::MoveAnchor);

  // insert the remaining text to give an illusion of unfolding
  blockCursor.insertText(userBlockData-&gt;getRemainingText());
  blockCursor.endEditBlock();

  userBlockData-&gt;setIsVisible(true);
  textBlock.setUserData(userBlockData);
} 
</code></pre>
<p dir="auto">any suggestion of recommendatins are welcome</p>
]]></description><link>https://forum.qt.io/topic/146167/store-qtextblock-user-data-without-deleting-it-after-undo-of-textedit-qml</link><generator>RSS for Node</generator><lastBuildDate>Fri, 23 Oct 2026 08:18:03 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/146167.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 03 Jul 2023 05:30:05 GMT</pubDate><ttl>60</ttl></channel></rss>