From d453f87513984749c41cebaf107bc9a46f81cd37 Mon Sep 17 00:00:00 2001 From: rekols Date: Mon, 3 Dec 2018 17:20:20 +0800 Subject: [PATCH] fix: restore the value of the scrollbar when refresh. Change-Id: Ib16c5f4dac64366156e74ae9d70eb55d159f377d --- src/editwrapper.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/editwrapper.cpp b/src/editwrapper.cpp index a1d6e8b3..3621d960 100644 --- a/src/editwrapper.cpp +++ b/src/editwrapper.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -143,24 +144,28 @@ void EditWrapper::refresh() } QFile file(filePath()); - int curPos = m_textEdit->textCursor().position(); - m_textEdit->setPlainText(QString()); if (file.open(QIODevice::ReadOnly)) { QTextStream out(&file); out.setCodec(m_textCodec); QString content = out.readAll(); + int curPos = m_textEdit->textCursor().position(); + int yoffset = m_textEdit->verticalScrollBar()->value(); + int xoffset = m_textEdit->horizontalScrollBar()->value(); + m_textEdit->setPlainText(QString()); m_textEdit->setUpdatesEnabled(false); m_textEdit->setPlainText(content); m_textEdit->setModified(false); QTextCursor textcur = m_textEdit->textCursor(); textcur.setPosition(curPos); - m_textEdit->setTextCursor(textcur); QTimer::singleShot(50, this, [=] { m_textEdit->setUpdatesEnabled(true); + m_textEdit->setTextCursor(textcur); + m_textEdit->verticalScrollBar()->setValue(yoffset); + m_textEdit->horizontalScrollBar()->setValue(xoffset); }); file.close();