QTextDocument doc; doc.setPlainText("Hello\nWorld"); // iterate over each block in the document QTextBlock block = doc.firstBlock(); while (block.isValid()) { QString text = block.text(); // get the text of the block QTextCharFormat format = block.charFormat(); // get the text format of the block QTextBlockFormat blockFormat = block.blockFormat(); // get the block format of the block qDebug() << "Block text:" << text; qDebug() << "Text alignment:" << blockFormat.alignment(); // move to next block block = block.next(); }
QTextDocument doc; QTextCursor cursor(&doc); cursor.insertHtml("This example shows how to get the second block in a QTextDocument and get information about the text format of the block, such as its font weight. The block is found using its line number.Hello
"); cursor.movePosition(QTextCursor::End); cursor.insertText("\nWorld"); // get the second block in the document QTextBlock block = doc.findBlockByLineNumber(1); QString text = block.text(); QTextCharFormat format = block.charFormat(); qDebug() << "Block text:" << text; qDebug() << "Font weight:" << format.fontWeight();