From 45ecbe1cbe00e147d2dd8ae5b71e3753cbe405bf Mon Sep 17 00:00:00 2001 From: Axel Spoerl Date: Thu, 10 Aug 2023 11:09:58 +0200 Subject: [PATCH] QPlainTextEdit: update placeholder text when document is empty When the placeholder text is changed after having been displayed, it doesn't get updated on the screen any more, unless the entire viewport is updated, e.g. because of a document change or a focus event. This patch simplifies QPlainTextEditPrivate::updatePlaceHolderVisibility() to update the visibility if the text document is empty. It replaces the member QPlainTextEditorPrivate::placeholderVisible by the function isPlaceHolderTextVisible(). It returns true, if the document is empty and a placeholder text exists, and otherwise false. It adapts and corrects tst_QPlainTextEdit::placeHolderVisibility(): - usage of new member function instead of data member. - do not expect an empty placeholder to be visible. Fixes: QTBUG-115831 Change-Id: Ic4427ce7f7f1b8cde89957b9de0b978bd34ba923 (cherry picked from commit 4c7cac682fddfad274325ff82b5f212543e5373a) Reviewed-by: Shawn Rutledge --- src/widgets/widgets/qplaintextedit.cpp | 14 +++----------- src/widgets/widgets/qplaintextedit_p.h | 6 +++++- .../widgets/qplaintextedit/tst_qplaintextedit.cpp | 6 +++--- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/widgets/widgets/qplaintextedit.cpp b/src/widgets/widgets/qplaintextedit.cpp index 485f0fd47e5..ef0e89f25d4 100644 --- a/src/widgets/widgets/qplaintextedit.cpp +++ b/src/widgets/widgets/qplaintextedit.cpp @@ -719,8 +719,7 @@ void QPlainTextEditPrivate::updateViewport() QPlainTextEditPrivate::QPlainTextEditPrivate() : tabChangesFocus(false), showCursorOnInitialShow(false), backgroundVisible(false), - centerOnScroll(false), inDrag(false), clickCausedFocus(false), placeholderVisible(true), - pageUpDownLastCursorYIsValid(false) + centerOnScroll(false), inDrag(false), clickCausedFocus(false), pageUpDownLastCursorYIsValid(false) { } @@ -788,14 +787,7 @@ void QPlainTextEditPrivate::_q_updatePlaceholderVisibility() // document that has changed (in _q_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. - bool placeholderCurrentyVisible = placeholderVisible; - - placeholderVisible = !placeholderText.isEmpty() - && q->document()->isEmpty() - && (!q->firstVisibleBlock().isValid() || - q->firstVisibleBlock().layout()->preeditAreaText().isEmpty()); - - if (placeholderCurrentyVisible != placeholderVisible) + if (q->document()->isEmpty()) viewport->update(); } @@ -1897,7 +1889,7 @@ void QPlainTextEdit::paintEvent(QPaintEvent *e) er.setRight(qMin(er.right(), maxX)); painter.setClipRect(er); - if (d->placeholderVisible) { + if (d->isPlaceHolderTextVisible()) { 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 68f471e3c44..6a5ef760803 100644 --- a/src/widgets/widgets/qplaintextedit_p.h +++ b/src/widgets/widgets/qplaintextedit_p.h @@ -128,7 +128,6 @@ public: uint centerOnScroll : 1; uint inDrag : 1; uint clickCausedFocus : 1; - uint placeholderVisible : 1; uint pageUpDownLastCursorYIsValid : 1; void setTopLine(int visualTopLine, int dx = 0); @@ -144,6 +143,11 @@ public: void _q_cursorPositionChanged(); void _q_modificationChanged(bool); + inline bool isPlaceHolderTextVisible() + { + Q_Q(QPlainTextEdit); + return q->document()->isEmpty() && !q->placeholderText().isEmpty(); + } }; QT_END_NAMESPACE diff --git a/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp b/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp index 37f869bb62e..ffcb35710e9 100644 --- a/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp +++ b/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp @@ -1829,7 +1829,7 @@ void tst_QPlainTextEdit::placeholderVisibility_data() QTest::addColumn>("setupCommands"); QTest::addColumn("placeholderVisible"); QTest::addRow("no placeholder set + no text set") - << QList{} << true; + << QList{} << false; QTest::addRow("no placeholder set + text set or text set + no placeholder set") << QList{ SetContent } << false; QTest::addRow("no placeholder set + text set + empty text set") @@ -1839,7 +1839,7 @@ void tst_QPlainTextEdit::placeholderVisibility_data() << QList{ ClearContent, SetContent } << false; QTest::addRow("empty placeholder set + no text set") - << QList{ ClearPlaceHolder } << true; + << QList{ ClearPlaceHolder } << false; QTest::addRow("empty placeholder set + text set") << QList{ ClearPlaceHolder, SetContent } << false; @@ -1916,7 +1916,7 @@ void tst_QPlainTextEdit::placeholderVisibility() plainTextEdit.show(); QVERIFY(QTest::qWaitForWindowExposed(&plainTextEdit)); - QTRY_VERIFY(plainTextEdit_d->placeholderVisible == placeholderVisible); + QTRY_VERIFY(plainTextEdit_d->isPlaceHolderTextVisible() == placeholderVisible); }