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 /src/widgets/widgets/qplaintextedit.cpp | |
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>
Diffstat (limited to 'src/widgets/widgets/qplaintextedit.cpp')
-rw-r--r-- | src/widgets/widgets/qplaintextedit.cpp | 40 |
1 files changed, 39 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())) { |