Results 1 to 4 of 4

Thread: QTextEdit restore cursor position

  1. #1
    Join Date
    May 2008
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTextEdit restore cursor position

    Hi.

    I have problem with saving/restoring cursor position after closing/opening my application.
    I use textCursor().position() to save position in QSettings and textCursor().setPosition() to restore it, but cursor always is on on position 0. Saving work's ok, if I look in settings file position looks correct.

    How to fix this ?

    Code (rest of code is based on Qt MDI example) :
    Qt Code:
    1. void MdiChild::setMdiWindowProperites(_editor_properites opt)
    2. {
    3. mdiWindowProperites = opt;
    4. setReadOnly(mdiWindowProperites.readOnly);
    5. setFont(QFont(mdiWindowProperites.fontName, mdiWindowProperites.fontSize, QFont::Normal));
    6. //editorOpt.lastDir = opt.lastDir;
    7.  
    8.  
    9. if(mdiWindowProperites.syntaxH)
    10. {
    11. if(highlighter <= 0)
    12. highlighter = new Highlighter(document(), mdiWindowProperites.syntaxHColors);
    13. if(highlighter > 0)
    14. highlighter->rehighlight();
    15. }
    16. else
    17. {
    18. if(highlighter > 0)
    19. delete(highlighter);
    20. highlighter = 0;
    21. };
    22.  
    23. textCursor().setPosition(mdiWindowProperites.cursorPosX);
    24. ensureCursorVisible();
    25. }
    To copy to clipboard, switch view to plain text mode 

    I use QT4.4.0 on openSUSE 10.3 KDE3.

    Artur

    PS.
    Sorry for my bad English.
    Last edited by jpn; 25th May 2008 at 15:09. Reason: missing [code] tags

  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: QTextEdit restore cursor position

    QTextEdit::textCursor() returns a copy. Try to do it like this:
    Qt Code:
    1. QTextCursor cursor = textCursor();
    2. cursor.setPosition(mdiWindowProperites.cursorPosX);
    3. setCursor(cursor);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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

    artur231 (25th May 2008)

  4. #3
    Join Date
    May 2008
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit restore cursor position

    Thanks.
    Now works great.

    setCursor(cursor);
    Must be :
    setTextCursor(cursor);


    Artur

  5. #4
    Join Date
    Aug 2008
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit restore cursor position

    Does anyone knows why this solution works?

    Have this somewhere in your code..
    // save cursor
    int cur_position = win.textCursor().position();
    One cannot use xxx.setPosition(cur_position) this on the object (QTextEdit) itself, but if one copy it to a local variable, change the local variable, and then write it it back, i can confirm this does work, but don't know why.

    /** ---------------------------------------------------------------------------
    * @brief display_gui::restore_cursor
    * @param win Reference to a QTextEdit object
    * @param cur_position The last known curser position
    */
    void display_gui::restore_cursor (QTextEdit & win, int cur_position)
    {
    #ifdef DEBUG_CURSOR_RESTORE
    fprintf (stderr,"..... b.NewPos %6d",win.textCursor().position());
    #endif

    //win.textCursor().setPosition(cur_position, QTextCursor::KeepAnchor); //QTextCursor::MoveAnchor);
    //win.textCursor().movePosition(QTextCursor::End, QTextCursor::KeepAnchor); //QTextCursor::MoveAnchor);
    // after many many hours .. this seem to be working !!!
    // for some UNKNOWN REASON you cannot do this to the original, but you can copy it, change
    // what you want , and write it back, then it works
    QTextCursor my_cur = win.textCursor();

    my_cur.setPosition(cur_position);
    win.setTextCursor(my_cur);

    #ifdef DEBUG_CURSOR_RESTORE
    fprintf (stderr,"..... b.Restore %6d", cur_position);
    #endif
    }
    What is also strange is that I tried to save the cursor in a QTextCursor variable, and restoring it, it does change the cursor position, but then one loose properties like underline and textcolor ??.

    Thanks

Similar Threads

  1. QTextEdit slow to insert text
    By thomaspu in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2008, 13:05
  2. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 07:03

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.