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>
This commit is contained in:
parent
ea8e48a679
commit
382faed8c7
@ -1319,6 +1319,35 @@ QTextDocument *QPlainTextEdit::document() const
|
||||
return d->control->document();
|
||||
}
|
||||
|
||||
/*!
|
||||
\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.
|
||||
*/
|
||||
@ -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())) {
|
||||
|
@ -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;
|
||||
|
||||
|
@ -179,6 +179,7 @@ public:
|
||||
void _q_modificationChanged(bool);
|
||||
|
||||
int originalOffsetY;
|
||||
QString placeholderText;
|
||||
};
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
Loading…
Reference in New Issue
Block a user