PDA

View Full Version : QTextEdit: how to get lines?



claudio
15th July 2007, 13:21
I have a QTextEdit and the user writes some text in it. How can I get the lines exactly the same way as they are displayed on the screen? I have to get every line start and line end - if possible the absolute text position *and* the y position of the line.

Thanks for any help!
Claudio

patrik08
15th July 2007, 14:49
I have a QTextEdit and the user writes some text in it. How can I get the lines exactly the same way as they are displayed on the screen? I have to get every line start and line end - if possible the absolute text position *and* the y position of the line.

Thanks for any help!
Claudio


here is a sample to get line ...
from http://juffed.googlecode.com/svn/trunk/qedit/qtedit.cpp

my own first projekt
http://www.qt-apps.org/content/show.php/QKeyWord+grep+Robot+-+Word-Completer?content=59422

as editor now i use only http://www.riverbankcomputing.co.uk/qscintilla/index.php having all



QString TextEdit::GrepTextLines(int startline, int stopline )
{
QStringList cuttextlist;
cuttextlist.clear();
int xli = 1;
for ( QTextBlock block = document()->begin(); block.isValid(); block = block.next(), xli++) {
if (xli == startline || xli > startline) {
if (xli == stopline || xli < stopline) {
cuttextlist.append(GrepLineFromTxBloc(block));
////////qDebug() << "### cat line GrepTextLines " << xli;
}
}
}
return cuttextlist.join("\n");
}

QString TextEdit::GrepLineFromTxBloc( QTextBlock block )
{
QString linetext = "";
if ( m_tabSpaces ) /* if tabulator key on to indent text ? */
{
int nbSpaces = tabStopWidth() / fontMetrics().width( " " );
for (int i = 0; i<nbSpaces; i++) {
linetext.append( " " );
}
linetext.append(block.text());

} else {
linetext.append(block.text());
}

return linetext;
}

claudio
15th July 2007, 21:08
Yea, great! This is my code for scanning the blocks on a mouse move event:


for(QTextBlock block = document()->begin(); block.isValid(); block = block.next()) {
if(block.contains(textpos)) {
int posinblock = textpos - block.position();
if(block.layout()->isValidCursorPosition(posinblock)) {
QTextLine tl = block.layout()->lineForTextPosition(posinblock);
int linestartpos = block.position() + tl.textStart();
emit onMouseMoveTextLineStartPos(linestartpos);
}
break;
}
}


Thanks for your help, patrik08!