PDA

View Full Version : QPlainTextEdit reload text



bunjee
25th April 2009, 12:34
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.

wagmare
25th April 2009, 12:59
check this thread in this forum
http://www.qtcentre.org/forum/f-qt-programming-2/t-problem-in-setting-cursor-position-in-readonly-qtextedit-19681.html/?highlight=QTextCursor

bunjee
25th April 2009, 15:24
Solved.


// Backup settings
int position = d->plainTextEdit->textCursor().position();

int vScrollPosition = d->plainTextEdit->verticalScrollBar()->value();
int hScrollPosition = d->plainTextEdit->horizontalScrollBar()->value();

// Getting content
QFile scriptFile(d->script->filePath());
scriptFile.open(QIODevice::ReadOnly);
d->plainTextEdit->setPlainText(scriptFile.readAll());
scriptFile.close();

// Restore settings
QTextCursor cursor = d->plainTextEdit->textCursor();
cursor.setPosition(position);
d->plainTextEdit->setTextCursor(cursor);

d->plainTextEdit->verticalScrollBar()->setValue(vScrollPosition);
d->plainTextEdit->horizontalScrollBar()->setValue(hScrollPosition);


Note: this resets the undo stack.