QTextDocument document; QTextCursor cursor(&document); // Create a new text cursor cursor.insertText("Hello, World!"); // Insert some text QTextBlock block = document.firstBlock(); // Get the first text block while(block.isValid()) { qDebug() << block.text(); // Print the text of the current block block = block.next(); // Move to the next block }
QTextEdit editor; QTextDocument* document = editor.document(); // Get the text editor's document QTextBlock block = document->begin(); // Get the first text block while(block.isValid()) { QTextCursor cursor(&block); cursor.select(QTextCursor::BlockUnderCursor); // Select the current block QString text = cursor.selectedText(); qDebug() << text; // Print the text of the current block block = block.next(); // Move to the next block }This code creates a new QTextEdit widget, gets the underlying QTextDocument, and then iterates over each QTextBlock using the begin function. The QTextCursor class is used to select the text of each block, and the selectedText function is called to get the text. The qDebug function is used to print the text of each block. Package: Qt Gui Library