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
Reviewed-by: Santhosh Kumar <santhosh.kumar.selvaraj@qt.io>
(cherry picked from commit 4c7cac682fddfad274325ff82b5f212543e5373a)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Axel Spoerl 2023-08-10 11:09:58 +02:00 committed by Qt Cherry-pick Bot
parent 5c0dea4760
commit f0869a66ae
3 changed files with 11 additions and 15 deletions

View File

@ -726,8 +726,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)
{
}
@ -800,14 +799,7 @@ void QPlainTextEditPrivate::updatePlaceholderVisibility()
// 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.
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();
}
@ -1910,7 +1902,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());

View File

@ -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 cursorPositionChanged();
void modificationChanged(bool);
inline bool isPlaceHolderTextVisible()
{
Q_Q(QPlainTextEdit);
return q->document()->isEmpty() && !q->placeholderText().isEmpty();
}
};
QT_END_NAMESPACE

View File

@ -1830,7 +1830,7 @@ void tst_QPlainTextEdit::placeholderVisibility_data()
QTest::addColumn<QList<SetupCommand>>("setupCommands");
QTest::addColumn<bool>("placeholderVisible");
QTest::addRow("no placeholder set + no text set")
<< QList<SetupCommand>{} << true;
<< QList<SetupCommand>{} << false;
QTest::addRow("no placeholder set + text set or text set + no placeholder set")
<< QList<SetupCommand>{ SetContent } << false;
QTest::addRow("no placeholder set + text set + empty text set")
@ -1840,7 +1840,7 @@ void tst_QPlainTextEdit::placeholderVisibility_data()
<< QList<SetupCommand>{ ClearContent, SetContent }
<< false;
QTest::addRow("empty placeholder set + no text set")
<< QList<SetupCommand>{ ClearPlaceHolder } << true;
<< QList<SetupCommand>{ ClearPlaceHolder } << false;
QTest::addRow("empty placeholder set + text set")
<< QList<SetupCommand>{ ClearPlaceHolder, SetContent }
<< false;
@ -1917,7 +1917,7 @@ void tst_QPlainTextEdit::placeholderVisibility()
plainTextEdit.show();
QVERIFY(QTest::qWaitForWindowExposed(&plainTextEdit));
QTRY_VERIFY(plainTextEdit_d->placeholderVisible == placeholderVisible);
QTRY_VERIFY(plainTextEdit_d->isPlaceHolderTextVisible() == placeholderVisible);
}