PDA

View Full Version : Getting start index and length of a line in QTextDocument



slandvogt
1st July 2010, 19:26
I am wondering what the correct way is to achieve the following: I would like to have the character indices of the start of a line and its length.
At the moment I am abusing (again) the textcursor for that:

m_pTextCursor is a QTextCursor


void CQtTextLayout::GetLayoutIndicesOfLine( int line, int *start, int *length )
{
if( m_pTextCursor )
{
m_pTextCursor->movePosition( QTextCursor::Start );
for( int i=0; i<line; i++)
{
m_pTextCursor->movePosition( QTextCursor::Down );
}
m_pTextCursor->movePosition( QTextCursor::StartOfLine);
*start = m_pTextCursor->position();
m_pTextCursor->movePosition( QTextCursor::EndOfLine);
*length = m_pTextCursor->position() - *start;
DebugLog( "QT GetLayoutIndicesOfLine %d %d/%d", line, *start, *length );
}
else
{
*start = 0;
*length = 0;
}
}

JohannesMunk
3rd July 2010, 12:13
When you ignore line wrapping then each line is a QTextBlock (http://doc.trolltech.com/latest/qtextblock.html). Have a look at its position and length methods.

Use the textdocuments findBlock .. functions to access those.

Hope, thats what you are looking for

Johannes