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

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

    Untested:

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


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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.