diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2021-07-05 14:39:18 +0200 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2021-07-06 23:09:51 +0200 |
commit | 0e6b31019f01c72ea3af3de31095f8269c7d7f30 (patch) | |
tree | 26cedf56ddfce11236f3e6a1acc2d26d7444ec22 /tests/auto/widgets | |
parent | 713fad1c588b09296e6a358ff2b15fc36b2bc640 (diff) |
Fix placement of placeholder text in QLineEdits with action icons
After dc794f7622bc00f7ca50fab65d6965695d6d2972, side widgets only got
space if they were not fading out, but the logic was not correctly
accounting for side widgets that never fade, such as buttons added via
QLineEdit::addAction.
Fix this to give visible widgets space, unless they are fading out. That
was the intent of the original change. Rename the variable to make its
purpose clearer, and reset it at the end of the fade-out animation.
Add a much-needed test that relies on private APIs to verify that the
effective margins are calculated correctly.
Fixes: QTBUG-94824
Pick-to: 6.2 6.1 5.15
Change-Id: If2ee6be52be9e4f9be1e91f72f27681ce27def6d
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Diffstat (limited to 'tests/auto/widgets')
-rw-r--r-- | tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp index 99344228b6..b3116491e3 100644 --- a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp +++ b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp @@ -301,6 +301,7 @@ private slots: void clearButtonVisibleAfterSettingText_QTBUG_45518(); void sideWidgets(); void sideWidgetsActionEvents(); + void sideWidgetsEffectiveMargins(); void shouldShowPlaceholderText_data(); void shouldShowPlaceholderText(); @@ -4737,6 +4738,71 @@ void tst_QLineEdit::sideWidgetsActionEvents() QCOMPARE(toolButton2->x(), toolButton1X); } +/*! + Verify that side widgets are positioned correctly and result in + correct effective text margins. +*/ +void tst_QLineEdit::sideWidgetsEffectiveMargins() +{ +#ifndef QT_BUILD_INTERNAL + QSKIP("This test requires a developer build."); +#else + QLineEdit edit; + edit.setPlaceholderText("placeholder"); + edit.setClearButtonEnabled(true); + edit.show(); + QLineEditPrivate *priv = QLineEditPrivate::get(&edit); + const auto sideWidgetParameters = priv->sideWidgetParameters(); + const int sideWidgetWidth = sideWidgetParameters.widgetWidth + sideWidgetParameters.margin; + QVERIFY(QTest::qWaitForWindowExposed(&edit)); + + QCOMPARE(priv->effectiveTextMargins().left(), 0); + QCOMPARE(priv->effectiveTextMargins().right(), 0); + + edit.setText("Left to right"); // clear button fades in on the right + QCOMPARE(priv->effectiveTextMargins().left(), 0); + QCOMPARE(priv->effectiveTextMargins().right(), sideWidgetWidth); + edit.clear(); + QCOMPARE(priv->effectiveTextMargins().left(), 0); + QCOMPARE(priv->effectiveTextMargins().right(), 0); + + edit.setLayoutDirection(Qt::RightToLeft); + edit.setText("ئۇيغۇر تىلى"); // clear button fades in on the left + QCOMPARE(priv->effectiveTextMargins().left(), sideWidgetWidth); + QCOMPARE(priv->effectiveTextMargins().right(), 0); + edit.clear(); + QCOMPARE(priv->effectiveTextMargins().left(), 0); + QCOMPARE(priv->effectiveTextMargins().right(), 0); + + edit.setLayoutDirection(Qt::LeftToRight); + + const QIcon leftIcon = edit.style()->standardIcon(QStyle::SP_FileIcon); + const QIcon rightIcon = edit.style()->standardIcon(QStyle::SP_DirIcon); + edit.addAction(leftIcon, QLineEdit::ActionPosition::LeadingPosition); + QCOMPARE(priv->effectiveTextMargins().left(), sideWidgetWidth); + QCOMPARE(priv->effectiveTextMargins().right(), 0); + + edit.addAction(rightIcon, QLineEdit::ActionPosition::TrailingPosition); + QCOMPARE(priv->effectiveTextMargins().left(), sideWidgetWidth); + QCOMPARE(priv->effectiveTextMargins().right(), sideWidgetWidth); + + edit.setText("Left to right"); // clear button on the right + QCOMPARE(priv->effectiveTextMargins().left(), sideWidgetWidth); + QCOMPARE(priv->effectiveTextMargins().right(), 2 * sideWidgetWidth); + edit.clear(); + QCOMPARE(priv->effectiveTextMargins().left(), sideWidgetWidth); + QCOMPARE(priv->effectiveTextMargins().right(), sideWidgetWidth); + + edit.setLayoutDirection(Qt::RightToLeft); + edit.setText("ئۇيغۇر تىلى"); // clear button fades in on the left + QCOMPARE(priv->effectiveTextMargins().left(), 2 * sideWidgetWidth); + QCOMPARE(priv->effectiveTextMargins().right(), sideWidgetWidth); + edit.clear(); + QCOMPARE(priv->effectiveTextMargins().left(), sideWidgetWidth); + QCOMPARE(priv->effectiveTextMargins().right(), sideWidgetWidth); +#endif +} + Q_DECLARE_METATYPE(Qt::AlignmentFlag) void tst_QLineEdit::shouldShowPlaceholderText_data() { |