From a2666d33919807ba6170f55f8e74a71ef8392a4d Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Mon, 2 Dec 2013 14:42:58 +0100 Subject: [PATCH] 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 Reviewed-by: Marc Mutz --- src/widgets/widgets/qlineedit.cpp | 7 ++- src/widgets/widgets/qlineedit_p.h | 3 +- .../widgets/qlineedit/tst_qlineedit.cpp | 59 ++++++++++++++++++- 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/src/widgets/widgets/qlineedit.cpp b/src/widgets/widgets/qlineedit.cpp index ee1bd315d2..1833dce40b 100644 --- a/src/widgets/widgets/qlineedit.cpp +++ b/src/widgets/widgets/qlineedit.cpp @@ -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. diff --git a/src/widgets/widgets/qlineedit_p.h b/src/widgets/widgets/qlineedit_p.h index 782feabac0..aa5b57a920 100644 --- a/src/widgets/widgets/qlineedit_p.h +++ b/src/widgets/widgets/qlineedit_p.h @@ -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) { diff --git a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp index a9f5cb686c..d6d48a1e8d 100644 --- a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp +++ b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp @@ -53,7 +53,7 @@ #include "qstandarditemmodel.h" #include #include "qstylehints.h" -#include +#include #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("text"); + QTest::addColumn("hasFocus"); + QTest::addColumn("alignment"); + QTest::addColumn("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"