QLineEdit: hide placeholder text when h-centered & focused

[ChangeLog][QtWidgets][QLineEdit] A blinking cursor in the middle
over horizontally centered placeholder text looks bad. Thus,
horizontally centered content is now considered as an exception
and the placeholder text is hidden when the line edit is focused.

Task-number: QTBUG-31669
Change-Id: I17aa1e6656673f81545a8437f90814b188ad484a
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
J-P Nurmi 2013-12-02 14:42:58 +01:00 committed by The Qt Project
parent fc5e948ea8
commit a2666d3391
3 changed files with 66 additions and 3 deletions

View File

@ -333,7 +333,12 @@ void QLineEdit::setText(const QString& text)
\brief the line edit's placeholder text
Setting this property makes the line edit display a grayed-out
placeholder text as long as the text() is empty.
placeholder text as long as the line edit is empty.
Normally, an empty line edit shows the placeholder text even
when it has focus. However, if the content is horizontally
centered, the placeholder text is not displayed under
the cursor when the line edit has focus.
By default, this property contains an empty string.

View File

@ -150,7 +150,8 @@ public:
}
inline bool shouldShowPlaceholderText() const
{
return control->text().isEmpty() && control->preeditAreaText().isEmpty();
return control->text().isEmpty() && control->preeditAreaText().isEmpty()
&& !((alignment & Qt::AlignHCenter) && q_func()->hasFocus());
}
static inline QLineEditPrivate *get(QLineEdit *lineEdit) {

View File

@ -53,7 +53,7 @@
#include "qstandarditemmodel.h"
#include <qpa/qplatformtheme.h>
#include "qstylehints.h"
#include <private/qguiapplication_p.h>
#include <private/qapplication_p.h>
#include "qclipboard.h"
#ifdef Q_OS_MAC
@ -300,6 +300,9 @@ private slots:
void clearButton();
void sideWidgets();
void shouldShowPlaceholderText_data();
void shouldShowPlaceholderText();
protected slots:
void editingFinished();
@ -4136,5 +4139,59 @@ void tst_QLineEdit::sideWidgets()
lineEdit->addAction(iconAction);
}
Q_DECLARE_METATYPE(Qt::AlignmentFlag)
void tst_QLineEdit::shouldShowPlaceholderText_data()
{
QTest::addColumn<QString>("text");
QTest::addColumn<bool>("hasFocus");
QTest::addColumn<Qt::AlignmentFlag>("alignment");
QTest::addColumn<bool>("shouldShowPlaceholderText");
QTest::newRow("empty, non-focused, left") << QString() << false << Qt::AlignLeft << true;
QTest::newRow("empty, focused, left") << QString() << true << Qt::AlignLeft << true;
QTest::newRow("non-empty, non-focused, left") << QStringLiteral("Qt") << false << Qt::AlignLeft << false;
QTest::newRow("non-empty, focused, left") << QStringLiteral("Qt") << true << Qt::AlignLeft << false;
QTest::newRow("empty, non-focused, center") << QString() << false << Qt::AlignHCenter << true;
QTest::newRow("empty, focused, center") << QString() << true << Qt::AlignHCenter << false;
QTest::newRow("non-empty, non-focused, center") << QStringLiteral("Qt") << false << Qt::AlignHCenter << false;
QTest::newRow("non-empty, focused, center") << QStringLiteral("Qt") << true << Qt::AlignHCenter << false;
QTest::newRow("empty, non-focused, right") << QString() << false << Qt::AlignRight << true;
QTest::newRow("empty, focused, right") << QString() << true << Qt::AlignRight << true;
QTest::newRow("non-empty, non-focused, right") << QStringLiteral("Qt") << false << Qt::AlignRight << false;
QTest::newRow("non-empty, focused, right") << QStringLiteral("Qt") << true << Qt::AlignRight << false;
}
void tst_QLineEdit::shouldShowPlaceholderText()
{
#ifndef QT_BUILD_INTERNAL
QSKIP("This test requires a developer build.");
#else
QFETCH(QString, text);
QFETCH(bool, hasFocus);
QFETCH(Qt::AlignmentFlag, alignment);
QFETCH(bool, shouldShowPlaceholderText);
QLineEdit lineEdit;
// avoid "Test input context to commit without focused object" warnings
lineEdit.setAttribute(Qt::WA_InputMethodEnabled, false);
if (hasFocus) {
lineEdit.show();
QApplicationPrivate::setFocusWidget(&lineEdit, Qt::NoFocusReason);
}
QCOMPARE(lineEdit.hasFocus(), hasFocus);
lineEdit.setText(text);
lineEdit.setAlignment(alignment);
QLineEditPrivate *priv = QLineEditPrivate::get(&lineEdit);
QCOMPARE(priv->shouldShowPlaceholderText(), shouldShowPlaceholderText);
#endif
}
QTEST_MAIN(tst_QLineEdit)
#include "tst_qlineedit.moc"