summaryrefslogtreecommitdiffstats
path: root/src/widgets/widgets/qtextedit.cpp
diff options
context:
space:
mode:
authorBenjamin Port <benjamin.port@ben2367.fr>2013-04-10 17:49:57 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-05-28 10:53:32 +0200
commitaedeb1cca6112962284c477d5a2c240519910891 (patch)
treef270af824dcb19d8fe6ba379d6270c47ab6bf8e9 /src/widgets/widgets/qtextedit.cpp
parent7ed15da3c1b9e2bb26f414a10bfc6e6d79d7cc7b (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/widgets/widgets/qtextedit.cpp')
-rw-r--r--src/widgets/widgets/qtextedit.cpp41
1 files changed, 41 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