From dd1bddaf3642eb4ce2e280a776045edb74ea1a9c Mon Sep 17 00:00:00 2001 From: Axel Spoerl Date: Thu, 21 Dec 2023 14:04:38 +0100 Subject: [PATCH] QPlainTextEdit: update viewport, when placeholder text disappears MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QPlainTextEditPrivate::updatePlaceholderVisibility() issued a full viewport update, when the document became empty and the placeholder text needed to be shown. No update was issued, when the placeholder text was replaced by a first text character entered. That relied on the assumption, that the placeholder text would disappear with the first text line being rendered (even if it has just one char). When the placeholder text covered multiple line, only the first of them disappeared. This patch adds a boolean to remember, that the placeholder text is shown. If that is the case and the first char is entered, a full update is issued, to remove all lines of the placeholder text. The boolean flag is cleared thereafter, to avoid unnecessary viewport updates. isPlaceHolderTextVisible() is renamed into placeHolderTextToBeShown(), because the method returns an instruction, rather than a state. tst_QPlainTextEdit::placeholderVisibility() is adapted to test the boolean flag, hence the real visibility of the placeholder text. That extends its scope to the bug at hand. Fixes: QTBUG-119808 Pick-to: 6.5 6.2 Change-Id: I07a7059ae619dc85d0c21247d829120e6afa7115 Reviewed-by: Christian Ehrlicher (cherry picked from commit 729e23f15f366f013da8e149fa95e618c9a95e13) Reviewed-by: Qt Cherry-pick Bot (cherry picked from commit d19d32a9126f40ce9e55a7bcd976df32a675db0a) --- src/widgets/widgets/qplaintextedit.cpp | 18 ++++++++++++------ src/widgets/widgets/qplaintextedit_p.h | 5 +++-- .../qplaintextedit/tst_qplaintextedit.cpp | 2 +- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/widgets/widgets/qplaintextedit.cpp b/src/widgets/widgets/qplaintextedit.cpp index 8a0caf5962f..134fc30964e 100644 --- a/src/widgets/widgets/qplaintextedit.cpp +++ b/src/widgets/widgets/qplaintextedit.cpp @@ -725,8 +725,14 @@ void QPlainTextEditPrivate::updateViewport() } QPlainTextEditPrivate::QPlainTextEditPrivate() - : tabChangesFocus(false), showCursorOnInitialShow(false), backgroundVisible(false), - centerOnScroll(false), inDrag(false), clickCausedFocus(false), pageUpDownLastCursorYIsValid(false) + : tabChangesFocus(false) + , showCursorOnInitialShow(false) + , backgroundVisible(false) + , centerOnScroll(false) + , inDrag(false) + , clickCausedFocus(false) + , pageUpDownLastCursorYIsValid(false) + , placeholderTextShown(false) { } @@ -793,14 +799,14 @@ void QPlainTextEditPrivate::init(const QString &txt) void QPlainTextEditPrivate::updatePlaceholderVisibility() { - Q_Q(QPlainTextEdit); - // We normally only repaint the part of view that contains text in the // document that has changed (in repaintContents). But the placeholder // text is not a part of the document, but is drawn on separately. So whenever // we either show or hide the placeholder text, we issue a full update. - if (q->document()->isEmpty()) + if (placeholderTextShown != placeHolderTextToBeShown()) { viewport->update(); + placeholderTextShown = placeHolderTextToBeShown(); + } } void QPlainTextEditPrivate::repaintContents(const QRectF &contentsRect) @@ -1902,7 +1908,7 @@ void QPlainTextEdit::paintEvent(QPaintEvent *e) er.setRight(qMin(er.right(), maxX)); painter.setClipRect(er); - if (d->isPlaceHolderTextVisible()) { + if (d->placeHolderTextToBeShown()) { const QColor col = d->control->palette().placeholderText().color(); painter.setPen(col); painter.setClipRect(e->rect()); diff --git a/src/widgets/widgets/qplaintextedit_p.h b/src/widgets/widgets/qplaintextedit_p.h index 6e4aa8133c2..d58b0d04f78 100644 --- a/src/widgets/widgets/qplaintextedit_p.h +++ b/src/widgets/widgets/qplaintextedit_p.h @@ -129,6 +129,7 @@ public: uint inDrag : 1; uint clickCausedFocus : 1; uint pageUpDownLastCursorYIsValid : 1; + uint placeholderTextShown : 1; void setTopLine(int visualTopLine, int dx = 0); void setTopBlock(int newTopBlock, int newTopLine, int dx = 0); @@ -143,9 +144,9 @@ public: void cursorPositionChanged(); void modificationChanged(bool); - inline bool isPlaceHolderTextVisible() + inline bool placeHolderTextToBeShown() const { - Q_Q(QPlainTextEdit); + Q_Q(const QPlainTextEdit); return q->document()->isEmpty() && !q->placeholderText().isEmpty(); } }; diff --git a/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp b/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp index ffcb35710e9..3a1d4143768 100644 --- a/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp +++ b/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp @@ -1916,7 +1916,7 @@ void tst_QPlainTextEdit::placeholderVisibility() plainTextEdit.show(); QVERIFY(QTest::qWaitForWindowExposed(&plainTextEdit)); - QTRY_VERIFY(plainTextEdit_d->isPlaceHolderTextVisible() == placeholderVisible); + QTRY_COMPARE(plainTextEdit_d->placeholderTextShown, placeholderVisible); }