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
Pick-to: 6.2
Change-Id: Ie658411664d88df6cb6a337376bdcf897e8c1790
Reviewed-by: Jan Arve Sæther <jan-arve.saether@qt.io>
This commit is contained in:
Jan Arve Sæther 2021-08-30 16:12:16 +02:00
parent aeb1d11f26
commit e0990f8b40
3 changed files with 14 additions and 19 deletions

View File

@ -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

View File

@ -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);

View File

@ -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]
*