[Qt-interest] Simple way to get one text line from QTextEdit widget

Forrest forrest37 at verizon.net
Mon Oct 26 23:51:45 CET 2009


Konstantin Tokarev wrote:
> I'm looking for a simple way to get text line with certain number from QTextEdit widget (probably, using its QTextDocument or QString representations). 
> Any ideas?

Simple I don't know about, but this might do it:

Find the QTextBlock containing desiredLineNumber by iterating over all 
the blocks and lineCount()ing them. Remember the number of the first 
line in the block.

Take the block's QTextLayout.

Subtract the first line number of the block from desiredLineNumber to 
get the offset of the desired line within the layout, and use that to 
obtain the relevant QTextLine using QTextLayout's lineAt() function.

QTextLine lacks a .text() function, so you'll need to dig it out of the 
QTextBlock:

QString lineText = desiredBlock.text().mid(desiredTextLine.textStart(), 
desiredTextLine.textLength());

[QTextLayout also claims to offer .text(), but it doesn't work for me.]



More information about the Qt-interest-old mailing list