QTextDocument doc; QTextCursor cursor(&doc); cursor.movePosition(QTextCursor::Start); cursor.movePosition(QTextCursor::NextBlock); cursor.insertText("This is the first line.\n"); cursor.insertText("This is the second line.\n"); cursor.insertText("This is the third line.\n"); QTextBlock block = doc.findBlock(10); // find the block at position 10 qDebug() << block.text(); // prints "is the first line."
QTextDocument doc; QTextCursor cursor(&doc); cursor.movePosition(QTextCursor::Start); cursor.movePosition(QTextCursor::NextBlock); cursor.insertText("apple\n"); cursor.insertText("banana\n"); cursor.insertText("cherry\n"); QTextBlock block = doc.findBlockByLineNumber(1); // find the second block (index 1) while (block.isValid()) { QString text = block.text(); if (text.startsWith("b")) { qDebug() << text; // prints "banana" } block = block.next(); }In this example, findBlockByLineNumber() is used to get a QTextBlock object for the second line of text ("banana"). The sample then loops through each subsequent block using the next() function, checking if each block's text starts with the letter "b". If so, it prints the block's text. These examples are written using the Qt framework in C++. The QTextDocument class belongs to the Qt GUI library.