Keep QLineEdit placeholder text visible when focused

This is useful when the widget is focused by default, and also gives
another opportunity to the user to read the message before typing,
removing the need for workarounds like clicking in another text entry to
bring back the placeholder message.

Change-Id: I4a2981a6656a87934b5c8ed4a8a2dc13b748c94d
Reviewed-by: Gabriel de Dietrich <gabriel.dedietrich@digia.com>
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Reviewed-by: Jens Bache-Wiig <jens.bache-wiig@digia.com>
This commit is contained in:
Aurélien Gâteau 2013-01-21 11:01:12 +01:00 committed by The Qt Project
parent 1580f55847
commit eeb31ad10a
2 changed files with 6 additions and 2 deletions

View File

@ -55,6 +55,7 @@ Window::Window()
echoComboBox->addItem(tr("No Echo")); echoComboBox->addItem(tr("No Echo"));
echoLineEdit = new QLineEdit; echoLineEdit = new QLineEdit;
echoLineEdit->setPlaceholderText("Placeholder Text");
echoLineEdit->setFocus(); echoLineEdit->setFocus();
//! [0] //! [0]
@ -68,6 +69,7 @@ Window::Window()
validatorComboBox->addItem(tr("Double validator")); validatorComboBox->addItem(tr("Double validator"));
validatorLineEdit = new QLineEdit; validatorLineEdit = new QLineEdit;
validatorLineEdit->setPlaceholderText("Placeholder Text");
//! [1] //! [1]
//! [2] //! [2]
@ -80,6 +82,7 @@ Window::Window()
alignmentComboBox->addItem(tr("Right")); alignmentComboBox->addItem(tr("Right"));
alignmentLineEdit = new QLineEdit; alignmentLineEdit = new QLineEdit;
alignmentLineEdit->setPlaceholderText("Placeholder Text");
//! [2] //! [2]
//! [3] //! [3]
@ -93,6 +96,7 @@ Window::Window()
inputMaskComboBox->addItem(tr("License key")); inputMaskComboBox->addItem(tr("License key"));
inputMaskLineEdit = new QLineEdit; inputMaskLineEdit = new QLineEdit;
inputMaskLineEdit->setPlaceholderText("Placeholder Text");
//! [3] //! [3]
//! [4] //! [4]
@ -104,6 +108,7 @@ Window::Window()
accessComboBox->addItem(tr("True")); accessComboBox->addItem(tr("True"));
accessLineEdit = new QLineEdit; accessLineEdit = new QLineEdit;
accessLineEdit->setPlaceholderText("Placeholder Text");
//! [4] //! [4]
//! [5] //! [5]

View File

@ -1801,7 +1801,7 @@ void QLineEdit::paintEvent(QPaintEvent *)
int minRB = qMax(0, -fm.minRightBearing()); int minRB = qMax(0, -fm.minRightBearing());
if (d->control->text().isEmpty()) { if (d->control->text().isEmpty()) {
if (!hasFocus() && !d->placeholderText.isEmpty()) { if (!d->placeholderText.isEmpty()) {
QColor col = pal.text().color(); QColor col = pal.text().color();
col.setAlpha(128); col.setAlpha(128);
QPen oldpen = p.pen(); QPen oldpen = p.pen();
@ -1810,7 +1810,6 @@ void QLineEdit::paintEvent(QPaintEvent *)
QString elidedText = fm.elidedText(d->placeholderText, Qt::ElideRight, lineRect.width()); QString elidedText = fm.elidedText(d->placeholderText, Qt::ElideRight, lineRect.width());
p.drawText(lineRect, va, elidedText); p.drawText(lineRect, va, elidedText);
p.setPen(oldpen); p.setPen(oldpen);
return;
} }
} }