diff options
author | Mikhail Svetkin <mikhail.svetkin@gmail.com> | 2013-12-26 22:41:39 +0600 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-01-23 07:53:53 +0100 |
commit | 382faed8c78335fe44828aee8ca6c0284bb9c9f7 (patch) | |
tree | 04130abbb261a901180981ff08e1daac1e61b116 | |
parent | ea8e48a6799cf742ea23f4a30dcfc38a4988fe56 (diff) |
Add a placeholderText property to QPlainTextEdit
Setting this property makes the editor display a grayed-out
placeholder text as long as the document() is empty.
Change-Id: I997edb867419613ff7cedc760a87c684a2ded711
Reviewed-by: David Faure <david.faure@kdab.com>
-rw-r--r-- | src/widgets/widgets/qplaintextedit.cpp | 40 | ||||
-rw-r--r-- | src/widgets/widgets/qplaintextedit.h | 4 | ||||
-rw-r--r-- | src/widgets/widgets/qplaintextedit_p.h | 1 |
3 files changed, 44 insertions, 1 deletions
diff --git a/src/widgets/widgets/qplaintextedit.cpp b/src/widgets/widgets/qplaintextedit.cpp index 2f576a4e39..d67e4735b3 100644 --- a/src/widgets/widgets/qplaintextedit.cpp +++ b/src/widgets/widgets/qplaintextedit.cpp @@ -1320,6 +1320,35 @@ QTextDocument *QPlainTextEdit::document() const } /*! + \since 5.3 + + \property QPlainTextEdit::placeholderText + \brief the editor placeholder text + + Setting this property makes the editor display a grayed-out + placeholder text as long as the document() is empty. + + By default, this property contains an empty string. + + \sa document() +*/ +void QPlainTextEdit::setPlaceholderText(const QString &placeholderText) +{ + Q_D(QPlainTextEdit); + if (d->placeholderText != placeholderText) { + d->placeholderText = placeholderText; + if (d->control->document()->isEmpty()) + d->viewport->update(); + } +} + +QString QPlainTextEdit::placeholderText() const +{ + Q_D(const QPlainTextEdit); + return d->placeholderText; +} + +/*! Sets the visible \a cursor. */ void QPlainTextEdit::setTextCursor(const QTextCursor &cursor) @@ -1945,7 +1974,16 @@ void QPlainTextEdit::paintEvent(QPaintEvent *e) } - layout->draw(&painter, offset, selections, er); + if (!placeholderText().isEmpty() && document()->isEmpty()) { + Q_D(QPlainTextEdit); + QColor col = d->control->palette().text().color(); + col.setAlpha(128); + painter.setPen(col); + const int margin = int(document()->documentMargin()); + painter.drawText(r.adjusted(margin, 0, 0, 0), Qt::AlignTop | Qt::TextWordWrap, placeholderText()); + } else { + layout->draw(&painter, offset, selections, er); + } if ((drawCursor && !drawCursorAsBlock) || (editable && context.cursorPosition < -1 && !layout->preeditAreaText().isEmpty())) { diff --git a/src/widgets/widgets/qplaintextedit.h b/src/widgets/widgets/qplaintextedit.h index 81548818ef..c276c23571 100644 --- a/src/widgets/widgets/qplaintextedit.h +++ b/src/widgets/widgets/qplaintextedit.h @@ -83,6 +83,7 @@ class Q_WIDGETS_EXPORT QPlainTextEdit : public QAbstractScrollArea Q_PROPERTY(int maximumBlockCount READ maximumBlockCount WRITE setMaximumBlockCount) Q_PROPERTY(bool backgroundVisible READ backgroundVisible WRITE setBackgroundVisible) Q_PROPERTY(bool centerOnScroll READ centerOnScroll WRITE setCenterOnScroll) + Q_PROPERTY(QString placeholderText READ placeholderText WRITE setPlaceholderText) public: enum LineWrapMode { NoWrap, @@ -96,6 +97,9 @@ public: void setDocument(QTextDocument *document); QTextDocument *document() const; + void setPlaceholderText(const QString &placeholderText); + QString placeholderText() const; + void setTextCursor(const QTextCursor &cursor); QTextCursor textCursor() const; diff --git a/src/widgets/widgets/qplaintextedit_p.h b/src/widgets/widgets/qplaintextedit_p.h index ab464676e5..3585b8307f 100644 --- a/src/widgets/widgets/qplaintextedit_p.h +++ b/src/widgets/widgets/qplaintextedit_p.h @@ -179,6 +179,7 @@ public: void _q_modificationChanged(bool); int originalOffsetY; + QString placeholderText; }; QT_END_NAMESPACE |