Results 1 to 6 of 6

Thread: [Qt4] QTextEdit & cursor visibility

  1. #1
    Join Date
    Oct 2006
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default [Qt4] QTextEdit & cursor visibility

    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
    Qt Code:
    1. //... doing things before ...
    2. //show file
    3. QTextEdit * editor = new QTextEdit(current_body_widget);
    4. editor->setFocus();
    5. {
    6. QString file_to_view = QDir::cleanPath(QString("%1%2%3").arg(build_dir).arg(QDir::separator()).arg(fp.file));
    7. QString error_str;
    8. QFile file( file_to_view );
    9. if ( !file.exists() ) {
    10. error_str = QString( "%1 does not exist." ).arg( file_to_view );
    11. }else{
    12. if ( !file.open( QFile::ReadOnly | QFile::Text ) ) {
    13. error_str = QString( "Cannot open %1" ).arg( file_to_view );
    14. }
    15. }
    16. if(error_str.isEmpty()){
    17. int nb_char=0;
    18. int pos = 0;
    19. int line = 0;
    20. editor->setPlainText( QString("") );
    21. QTextStream in(&file);
    22.  
    23. while(!in.atEnd()){
    24. line++;
    25. if(line == pi.line)
    26. pos = nb_char;
    27. QString line = in.readLine();
    28. editor->append(line);
    29. nb_char+= line.size() + 1; //+1 : '\n'
    30. }
    31.  
    32. //moving cursor
    33. QTextCursor text_cursor = editor->textCursor();
    34. text_cursor.setPosition(pos);
    35.  
    36. text_cursor.select(QTextCursor::BlockUnderCursor);
    37. editor->ensureCursorVisible();
    38. editor->setTextCursor(text_cursor);
    39. }
    40. else
    41. editor->setPlainText( error_str );
    42. //editor->setReadOnly(true);
    43. }
    44.  
    45. editor->update();
    46. layout->addWidget(editor);
    47. //... doing things after ...
    To copy to clipboard, switch view to plain text mode 

    What am i doing wrong ?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: [Qt4] QTextEdit & cursor visibility

    Try swapping these two lines (set the modified cursor first, then ensure it's visible):
    Qt Code:
    1. editor->ensureCursorVisible();
    2. editor->setTextCursor(text_cursor);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Oct 2006
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: [Qt4] QTextEdit & cursor visibility

    Well, it does not work. The line is still highlighted but only the begining of the file is visible

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: [Qt4] QTextEdit & cursor visibility

    Ok, I see the problem. QWidget's geometry is calculated upon first show event. You should move the ensureCursorVisible() call in QTextEdit's showEvent() or into a slot connected to a single shot timer with timeout 0. This ensures that the geometry is constructed properly before trying to do operations which rely on widget's geometry.

    Either subclass QTextEdit and override it's showEvent():
    Qt Code:
    1. void MyTextEdit::showEvent(QShowEvent* event)
    2. {
    3. QTextEdit::showEvent(event);
    4.  
    5. static bool initialized = false;
    6. if (!initialized)
    7. {
    8. ensureCursorVisible();
    9. initialized = true;
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    Or use the timer technique:
    Qt Code:
    1. // add this in place where you had "editor->ensureCursorVisible();"
    2. QTimer::singleShot(0, this, SLOT(initialize()));
    3.  
    4. // declared as a slot
    5. void BodyWidget::initialize()
    6. {
    7. // QTextEdit* editor moved as a member variable
    8. editor->ensureCursorVisible();
    9. }
    10. editor->ensureCursorVisible();
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    Ti_Thom (10th October 2006)

  6. #5
    Join Date
    Oct 2006
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: [Qt4] QTextEdit & cursor visibility

    I was also thinking about the widget's geometry computation and tryed to put "editor->ensureCursorVisible();" at the end of the BodyWidget build function, the visible text was not the right part.

    I just tryed your 2nd technique, it seems to work as expected. Thanks !

  7. #6

    Default Re: [Qt4] QTextEdit & cursor visibility

    QTextCursor* highlightCursor;
    QTextEdit *ui_textEdit;
    ...
    ui_textEdit->setTextCursor(*highlightCursor);
    ui_textEdit->ensureCursorVisible();

    just make sure u set the cursor using setTextCursor instead of ui_textEdit->textCursor().setPosition() and u call ensureCursorVisible after that.

    Himanshu Dahiya

Similar Threads

  1. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 06:03
  2. [Qt4] Can QTextEdit be overflowed?
    By naresh in forum Qt Programming
    Replies: 5
    Last Post: 14th March 2006, 13:28
  3. [QT 4] QTextEdit performance
    By fellobo in forum Qt Programming
    Replies: 8
    Last Post: 6th March 2006, 19:27
  4. Painting to QTextEdit
    By gesslar in forum Qt Programming
    Replies: 8
    Last Post: 18th February 2006, 18:40
  5. Obtaining clean (x)html from QTextEdit
    By ccf_h in forum Qt Programming
    Replies: 1
    Last Post: 5th February 2006, 14:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.