diff options
author | Benjamin Port <benjamin.port@ben2367.fr> | 2013-04-10 17:49:57 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-05-28 10:53:32 +0200 |
commit | aedeb1cca6112962284c477d5a2c240519910891 (patch) | |
tree | f270af824dcb19d8fe6ba379d6270c47ab6bf8e9 /src | |
parent | 7ed15da3c1b9e2bb26f414a10bfc6e6d79d7cc7b (diff) |
Added a placeholderText property to QTextEdit.
This commit adds a placeholder text for text edit. If text edit doesn't
have focus and the document() is empty, this placeholder will be shown.
Change-Id: I8b51f1a246452b63d6390e94853cf2864a7ff05f
Reviewed-by: David Faure (KDE) <faure@kde.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/widgets/widgets/qtextedit.cpp | 41 | ||||
-rw-r--r-- | src/widgets/widgets/qtextedit.h | 4 | ||||
-rw-r--r-- | src/widgets/widgets/qtextedit_p.h | 2 |
3 files changed, 47 insertions, 0 deletions
diff --git a/src/widgets/widgets/qtextedit.cpp b/src/widgets/widgets/qtextedit.cpp index 4b3bf6de65..da4998ca71 100644 --- a/src/widgets/widgets/qtextedit.cpp +++ b/src/widgets/widgets/qtextedit.cpp @@ -773,6 +773,36 @@ QTextDocument *QTextEdit::document() const } /*! + \since 5.2 + + \property QTextEdit::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 and the widget doesn't + have focus. + + By default, this property contains an empty string. + + \sa document() +*/ +QString QTextEdit::placeholderText() const +{ + Q_D(const QTextEdit); + return d->placeholderText; +} + +void QTextEdit::setPlaceholderText(const QString &placeholderText) +{ + Q_D(QTextEdit); + if (d->placeholderText != placeholderText) { + d->placeholderText = placeholderText; + if (!hasFocus() && d->control->document()->isEmpty()) + d->viewport->update(); + } +} + +/*! Sets the visible \a cursor. */ void QTextEdit::setTextCursor(const QTextCursor &cursor) @@ -1499,6 +1529,13 @@ void QTextEdit::paintEvent(QPaintEvent *e) Q_D(QTextEdit); QPainter p(d->viewport); d->paint(&p, e); + if (!d->placeholderText.isEmpty() && !hasFocus() && d->control->document()->isEmpty()) { + QColor col = palette().text().color(); + col.setAlpha(128); + p.setPen(col); + const int margin = int(document()->documentMargin()); + p.drawText(d->viewport->rect().adjusted(margin, margin, -margin, -margin), Qt::AlignTop | Qt::TextWordWrap, d->placeholderText); + } } void QTextEditPrivate::_q_currentCharFormatChanged(const QTextCharFormat &fmt) @@ -1712,6 +1749,8 @@ void QTextEdit::focusInEvent(QFocusEvent *e) } QAbstractScrollArea::focusInEvent(e); d->sendControlEvent(e); + if (!d->placeholderText.isEmpty() && d->control->document()->isEmpty()) + d->viewport->update(); } /*! \reimp @@ -1721,6 +1760,8 @@ void QTextEdit::focusOutEvent(QFocusEvent *e) Q_D(QTextEdit); QAbstractScrollArea::focusOutEvent(e); d->sendControlEvent(e); + if (!d->placeholderText.isEmpty() && d->control->document()->isEmpty()) + d->viewport->update(); } /*! \reimp diff --git a/src/widgets/widgets/qtextedit.h b/src/widgets/widgets/qtextedit.h index e1471848e7..06ec5fb889 100644 --- a/src/widgets/widgets/qtextedit.h +++ b/src/widgets/widgets/qtextedit.h @@ -85,6 +85,7 @@ class Q_WIDGETS_EXPORT QTextEdit : public QAbstractScrollArea Q_PROPERTY(int cursorWidth READ cursorWidth WRITE setCursorWidth) Q_PROPERTY(Qt::TextInteractionFlags textInteractionFlags READ textInteractionFlags WRITE setTextInteractionFlags) Q_PROPERTY(QTextDocument *document READ document WRITE setDocument DESIGNABLE false) + Q_PROPERTY(QString placeholderText READ placeholderText WRITE setPlaceholderText) public: enum LineWrapMode { NoWrap, @@ -108,6 +109,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/qtextedit_p.h b/src/widgets/widgets/qtextedit_p.h index 1ce68dc23e..daa6eb5ed0 100644 --- a/src/widgets/widgets/qtextedit_p.h +++ b/src/widgets/widgets/qtextedit_p.h @@ -130,6 +130,8 @@ public: QString anchorToScrollToWhenVisible; + QString placeholderText; + #ifdef QT_KEYPAD_NAVIGATION QBasicTimer deleteAllTimer; #endif |