From bbdcc86c52d806ab45fabf167526f9dcec291648 Mon Sep 17 00:00:00 2001 From: Wang Chuan Date: Sun, 7 Feb 2021 11:05:23 +0800 Subject: [PATCH] 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 (cherry picked from commit 13ae47d98057c2ddca8c865b845973d7e4c8dd8b) Reviewed-by: Qt Cherry-pick Bot --- src/widgets/widgets/qplaintextedit.cpp | 1 + .../qplaintextedit/tst_qplaintextedit.cpp | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/widgets/widgets/qplaintextedit.cpp b/src/widgets/widgets/qplaintextedit.cpp index 5f4b46c1cc0..d2006d05628 100644 --- a/src/widgets/widgets/qplaintextedit.cpp +++ b/src/widgets/widgets/qplaintextedit.cpp @@ -2306,6 +2306,7 @@ void QPlainTextEdit::showEvent(QShowEvent *) d->showCursorOnInitialShow = false; ensureCursorVisible(); } + d->_q_adjustScrollbars(); } /*! \reimp diff --git a/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp b/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp index 0cffe14ee4f..297eb5ec51a 100644 --- a/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp +++ b/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp @@ -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"