QPlainTextEdit: adjust scroll bars when showing up

The text of QPlainTextEdit might change when it is invisible, so an
adjustment of scroll bars is needed when the QPlainTextEdit showing
up, otherwise the range of scroll bars might be incorrect.

Fixes: QTBUG-77937
Change-Id: I45c686c7e09ca7b2944c36122e9157de0ec4f0e0
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
(cherry picked from commit 13ae47d98057c2ddca8c865b845973d7e4c8dd8b)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Wang Chuan 2021-02-07 11:05:23 +08:00 committed by Qt Cherry-pick Bot
parent 56981d3073
commit cc5a1473f1
2 changed files with 33 additions and 0 deletions

View File

@ -2306,6 +2306,7 @@ void QPlainTextEdit::showEvent(QShowEvent *)
d->showCursorOnInitialShow = false;
ensureCursorVisible();
}
d->_q_adjustScrollbars();
}
/*! \reimp

View File

@ -153,6 +153,7 @@ private slots:
#ifndef QT_NO_CLIPBOARD
void updateCursorPositionAfterEdit();
#endif
void appendTextWhenInvisible();
private:
void createSelection();
@ -1805,5 +1806,36 @@ void tst_QPlainTextEdit::updateCursorPositionAfterEdit()
}
#endif
void tst_QPlainTextEdit::appendTextWhenInvisible()
{
QWidget window;
window.resize(640, 480);
QPlainTextEdit *plainTextEdit = new QPlainTextEdit(&window);
plainTextEdit->resize(320, 240);
window.show();
QVERIFY(QTest::qWaitForWindowActive(&window));
// this should be long enough to let vertical scroll bar show up
const QString baseText("text\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\ntext");
const QString textToAppend("aaa");
plainTextEdit->setPlainText(baseText + "\n" + textToAppend);
const auto maxAfterSet = plainTextEdit->verticalScrollBar()->maximum();
// make sure the vertical scroll bar is visible
QVERIFY(maxAfterSet != 0);
plainTextEdit->clear();
plainTextEdit->setPlainText(baseText);
plainTextEdit->hide();
plainTextEdit->appendPlainText(textToAppend);
plainTextEdit->show();
const auto maxAfterAppend = plainTextEdit->verticalScrollBar()->maximum();
QVERIFY(maxAfterAppend != 0);
QCOMPARE(maxAfterAppend, maxAfterSet);
}
QTEST_MAIN(tst_QPlainTextEdit)
#include "tst_qplaintextedit.moc"