Skip to content

Commit

Permalink
fix: restore the value of the scrollbar when refresh.
Browse files Browse the repository at this point in the history
Change-Id: Ib16c5f4dac64366156e74ae9d70eb55d159f377d
  • Loading branch information
rekols committed Dec 3, 2018
1 parent 44a8f88 commit d453f87
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/editwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <QCoreApplication>
#include <QApplication>
#include <QSaveFile>
#include <QScrollBar>
#include <QDebug>
#include <QTimer>
#include <QDir>
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit d453f87

Please sign in to comment.