Important: Please read the Qt Code of Conduct -

Changing QTextBlock behavior for a QTextDocument

  • The documentation says that a QTextDocument is compose of QTextBlock.
    Every QTextBlock is determine by a sequence of characters ending by the special character \n.

    But I can't seem to find how exactly it would be possible to change this behavior.
    For instance, I would like to define a QTextBlock as "one or more consecutive lines of text, separated by one or more blank lines", just like Markdown paragraph.

    Is there way to change the behavior of a QTextDocument so it produces QTextBlock the way I want?

  • Moderators

    you are talking about the toPlainText() method right?

    A possibility is to traverse the QTextDocument yourself and generate the string (QTextBlock::text()) and insert your custom block separators.


    QString text;
    QTextBlock block = textDoc->firstBlock();
    while( block.isValid() )
         text.append( block.text() );
         text.append( MY_SEPARATOR_STRING );
         block =;

  • @raven-worx Thank for the idea! I'll dig into it!

Log in to reply