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