aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Arve Sæther <jan-arve.saether@qt.io>2021-08-30 16:12:16 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-09-06 19:23:08 +0000
commitfbe68ba784ff4bc499fda222a2d8556989e20e07 (patch)
treed0c6bd302126c231eacb527cdef1f05a987c4abb
parentdbcc2e7d56fca7cfe9209d1cb5b0640a0d3203d0 (diff)
Make TextField.implicitWidth fixed
Previously, implicitWidth was based on contentSize() (which was based on the full textwidth inside the TextField) This had the unfortunate consequence that an empty TextField (which is very normal) was very narrow on startup (18 pixels on macOS style), but would grow wider as the user kept on typing. When the TextField was in a layout, this would in addition lead to a relayout, even though it usually was barely noticeable. This change aligns that behavior to the other non-native styles. This also fixes a test on native style windows: TextField::test_implicitWidth where it assumes that the implicitWidth of TextField without a background is simply equal to left+right padding. This failed because the calculation of the geometry of the placeholder item was wrong. Task-number: QTBUG-95679 Change-Id: Ie658411664d88df6cb6a337376bdcf897e8c1790 Reviewed-by: Jan Arve Sæther <jan-arve.saether@qt.io> (cherry picked from commit e0990f8b4069ca912de5a27df00a8039aa4a4731) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/quicknativestyle/controls/DefaultTextField.qml15
-rw-r--r--src/quicknativestyle/items/qquickstyleitemtextfield.cpp8
-rw-r--r--tests/auto/quickcontrols2/controls/windows/BLACKLIST10
3 files changed, 14 insertions, 19 deletions
diff --git a/src/quicknativestyle/controls/DefaultTextField.qml b/src/quicknativestyle/controls/DefaultTextField.qml
index 8f8c8e4d0a..f9279974df 100644
--- a/src/quicknativestyle/controls/DefaultTextField.qml
+++ b/src/quicknativestyle/controls/DefaultTextField.qml
@@ -45,9 +45,9 @@ T.TextField {
readonly property bool __nativeBackground: background instanceof NativeStyle.StyleItem
- implicitWidth: Math.max(Math.max(contentWidth, placeholder.implicitWidth) + leftPadding + rightPadding,
- implicitBackgroundWidth + leftInset + rightInset,
- 90 /* minimum */ )
+ implicitWidth: implicitBackgroundWidth + leftInset + rightInset
+ || Math.max(contentWidth, placeholder.implicitWidth) + leftPadding + rightPadding
+
implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
contentHeight + topPadding + bottomPadding,
placeholder.implicitHeight + topPadding + bottomPadding)
@@ -67,11 +67,10 @@ T.TextField {
PlaceholderText {
id: placeholder
- height: control.height
- topPadding: control.topPadding
- bottomPadding: control.bottomPadding
- leftPadding: control.leftPadding
- rightPadding: control.rightPadding
+ x: control.leftPadding
+ y: control.topPadding
+ width: control.availableWidth
+ height: control.availableHeight
text: control.placeholderText
font: control.font
color: control.placeholderTextColor
diff --git a/src/quicknativestyle/items/qquickstyleitemtextfield.cpp b/src/quicknativestyle/items/qquickstyleitemtextfield.cpp
index cd7d35aa67..0a5eabeb8d 100644
--- a/src/quicknativestyle/items/qquickstyleitemtextfield.cpp
+++ b/src/quicknativestyle/items/qquickstyleitemtextfield.cpp
@@ -56,7 +56,13 @@ StyleItemGeometry QQuickStyleItemTextField::calculateGeometry()
StyleItemGeometry geometry;
geometry.minimumSize = style()->sizeFromContents(QStyle::CT_LineEdit, &styleOption, QSize(0, 0));
- geometry.implicitSize = style()->sizeFromContents(QStyle::CT_LineEdit, &styleOption, contentSize());
+
+ // Inspired by QLineEdit::sizeHint()
+ QFontMetricsF fm(styleFont(const_cast<QQuickItem*>(control<QQuickItem>())));
+ const QSize sz(qCeil(fm.horizontalAdvance(QLatin1Char('x')) * 17),
+ contentSize().height());
+ geometry.implicitSize = style()->sizeFromContents(QStyle::CT_LineEdit, &styleOption, sz);
+
styleOption.rect = QRect(QPoint(0, 0), geometry.implicitSize);
geometry.layoutRect = styleOption.rect;
geometry.contentRect = style()->subElementRect(QStyle::SE_LineEditContents, &styleOption);
diff --git a/tests/auto/quickcontrols2/controls/windows/BLACKLIST b/tests/auto/quickcontrols2/controls/windows/BLACKLIST
index 422683648a..b69aa99619 100644
--- a/tests/auto/quickcontrols2/controls/windows/BLACKLIST
+++ b/tests/auto/quickcontrols2/controls/windows/BLACKLIST
@@ -17,14 +17,6 @@
*
[ComboBox::test_font]
*
-[ComboBox::test_implicitContentWidthPolicy_WidestText:Array]
-*
-[ComboBox::test_implicitContentWidthPolicy_WidestText:ListModel]
-*
-[ComboBox::test_implicitContentWidthPolicy_WidestTextWhenCompleted:Array]
-*
-[ComboBox::test_implicitContentWidthPolicy_WidestTextWhenCompleted:ListModel]
-*
[Popup::test_font]
*
[ScrollBar::test_flashing]
@@ -89,5 +81,3 @@
*
[TextArea::test_implicitSize]
*
-[TextField::test_implicitSize]
-*