Results 1 to 3 of 3

Thread: QPlainTextEdit reload text

  1. #1
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default QPlainTextEdit reload text

    Hey trolls,

    I have a QPlainTextEdit.
    I've load the content of a file.

    Now in another editor I modify the content of that file.

    Is it possible to get my QPlainTextEdit refreshed without getting my cursor / scrollbars screwed up ?

    Thanks.

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPlainTextEdit reload text


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

    bunjee (25th April 2009)

  4. #3
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QPlainTextEdit reload text

    Solved.

    Qt Code:
    1. // Backup settings
    2. int position = d->plainTextEdit->textCursor().position();
    3.  
    4. int vScrollPosition = d->plainTextEdit->verticalScrollBar()->value();
    5. int hScrollPosition = d->plainTextEdit->horizontalScrollBar()->value();
    6.  
    7. // Getting content
    8. QFile scriptFile(d->script->filePath());
    9. scriptFile.open(QIODevice::ReadOnly);
    10. d->plainTextEdit->setPlainText(scriptFile.readAll());
    11. scriptFile.close();
    12.  
    13. // Restore settings
    14. QTextCursor cursor = d->plainTextEdit->textCursor();
    15. cursor.setPosition(position);
    16. d->plainTextEdit->setTextCursor(cursor);
    17.  
    18. d->plainTextEdit->verticalScrollBar()->setValue(vScrollPosition);
    19. d->plainTextEdit->horizontalScrollBar()->setValue(hScrollPosition);
    To copy to clipboard, switch view to plain text mode 

    Note: this resets the undo stack.

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

    johnmauer (2nd February 2010)

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Replies: 0
    Last Post: 10th April 2009, 15:28
  3. Match the text beetween two string
    By dreamer in forum Qt Programming
    Replies: 4
    Last Post: 20th May 2008, 14:48
  4. QTextEdit slow to insert text
    By thomaspu in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2008, 12:05
  5. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 15:30

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.