Results 1 to 3 of 3

Thread: Restoring cursor position in QTextEdit

  1. #1

    Default Restoring cursor position in QTextEdit

    Hi people,

    I am trying to save to current cursor position, which i want to restore later on. It seems that restoring does not update the actual cursor. Here is a code snippet:

    Qt Code:
    1. // in constuctor
    2. pTextCursor = new QTextCursor(this->document());
    3.  
    4. void CCodeWidgetText::saveCursor()
    5. {
    6. this->cursorPos = this->pTextCursor->position();
    7. }
    8.  
    9. void CCodeWidgetText::restoreCursor()
    10. {
    11. pTextCursor->setPosition(this->cursorPos, QTextCursor::MoveAnchor);
    12. //this->setTextCursor(*pTextCursor);
    13. }
    To copy to clipboard, switch view to plain text mode 

    Before this I tried to save the whole QTextCursor object and later on restore it, but that didn't seem to work either. Can someone help me out?

    Thanks in advance
    Last edited by jpn; 30th May 2008 at 13:53. 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: Restoring cursor position in QTextEdit

    Qt Code:
    1. // pTextCursor = new QTextCursor(this->document()); // forget this
    2.  
    3. void CCodeWidgetText::saveCursor()
    4. {
    5. this->cursorPos = this->textCursor().position();
    6. }
    7.  
    8. void CCodeWidgetText::restoreCursor()
    9. {
    10. QTextCursor cursor = this->textCursor();
    11. cursor.setPosition(this->cursorPos, QTextCursor::MoveAnchor);
    12. this->setTextCursor(cursor);
    13. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3

    Default Re: Restoring cursor position in QTextEdit

    Thanks, it works now

Similar Threads

  1. QTextEdit slow to insert text
    By thomaspu in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2008, 12:05
  2. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 06: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.