QPlainTextEdit: update viewport, when placeholder text disappears

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 <ch.ehrlicher@gmx.de>
(cherry picked from commit 729e23f15f366f013da8e149fa95e618c9a95e13)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit d19d32a9126f40ce9e55a7bcd976df32a675db0a)
This commit is contained in:
Axel Spoerl 2023-12-21 14:04:38 +01:00 committed by Qt Cherry-pick Bot
parent 091b5a2bb4
commit dd1bddaf36
3 changed files with 16 additions and 9 deletions

View File

@ -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());

View File

@ -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();
}
};

View File

@ -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);
}