Ti_Thom
10th October 2006, 16:12
Hi,
i am writting a part of a widget that have to display a file and then highlight a line off this file.
The file is properly loaded and the wanted line highlighted but not visible : the QTextEdit widget does not scroll to the cursor position ...
the code is
//... doing things before ...
//show file
QTextEdit * editor = new QTextEdit(current_body_widget);
editor->setFocus();
{
QString file_to_view = QDir::cleanPath(QString("%1%2%3").arg(build_dir).arg(QDir::separator()).arg(fp.fil e));
QString error_str;
QFile file( file_to_view );
if ( !file.exists() ) {
error_str = QString( "%1 does not exist." ).arg( file_to_view );
}else{
if ( !file.open( QFile::ReadOnly | QFile::Text ) ) {
error_str = QString( "Cannot open %1" ).arg( file_to_view );
}
}
if(error_str.isEmpty()){
int nb_char=0;
int pos = 0;
int line = 0;
editor->setPlainText( QString("") );
QTextStream in(&file);
while(!in.atEnd()){
line++;
if(line == pi.line)
pos = nb_char;
QString line = in.readLine();
editor->append(line);
nb_char+= line.size() + 1; //+1 : '\n'
}
//moving cursor
QTextCursor text_cursor = editor->textCursor();
text_cursor.setPosition(pos);
text_cursor.select(QTextCursor::BlockUnderCursor);
editor->ensureCursorVisible();
editor->setTextCursor(text_cursor);
}
else
editor->setPlainText( error_str );
//editor->setReadOnly(true);
}
editor->update();
layout->addWidget(editor);
//... doing things after ...
What am i doing wrong ?
i am writting a part of a widget that have to display a file and then highlight a line off this file.
The file is properly loaded and the wanted line highlighted but not visible : the QTextEdit widget does not scroll to the cursor position ...
the code is
//... doing things before ...
//show file
QTextEdit * editor = new QTextEdit(current_body_widget);
editor->setFocus();
{
QString file_to_view = QDir::cleanPath(QString("%1%2%3").arg(build_dir).arg(QDir::separator()).arg(fp.fil e));
QString error_str;
QFile file( file_to_view );
if ( !file.exists() ) {
error_str = QString( "%1 does not exist." ).arg( file_to_view );
}else{
if ( !file.open( QFile::ReadOnly | QFile::Text ) ) {
error_str = QString( "Cannot open %1" ).arg( file_to_view );
}
}
if(error_str.isEmpty()){
int nb_char=0;
int pos = 0;
int line = 0;
editor->setPlainText( QString("") );
QTextStream in(&file);
while(!in.atEnd()){
line++;
if(line == pi.line)
pos = nb_char;
QString line = in.readLine();
editor->append(line);
nb_char+= line.size() + 1; //+1 : '\n'
}
//moving cursor
QTextCursor text_cursor = editor->textCursor();
text_cursor.setPosition(pos);
text_cursor.select(QTextCursor::BlockUnderCursor);
editor->ensureCursorVisible();
editor->setTextCursor(text_cursor);
}
else
editor->setPlainText( error_str );
//editor->setReadOnly(true);
}
editor->update();
layout->addWidget(editor);
//... doing things after ...
What am i doing wrong ?