diff options
author | Mitch Curtis <mitch.curtis@qt.io> | 2018-12-14 12:08:48 +0100 |
---|---|---|
committer | Mitch Curtis <mitch.curtis@qt.io> | 2018-12-21 12:01:23 +0000 |
commit | b563bd583a50442b52e47f8c5f149aebafb385f2 (patch) | |
tree | ee6347d0c2894309b8b2b2299de7901b59123dd5 /src/quicktemplates2/qquicktextfield.cpp | |
parent | 69f02184a9a71f3a5e2dd1ada12367ddccd29787 (diff) |
Fix TextField background not respecting control's width
After 6858d4e9, the background of the Material style TextField does not respect
the width of the control if the control is resized (e.g. due to being in a
layout).
- The first time resizeBackground() is called, !extra.isAllocated() returns
true - that is, extra has not been allocated yet.
- The if statement is executed and the background's width is set to the
available width of the control (e.g. 120).
- As a result of the background's width being set, its widthValid flag is set
to true. This would previously be undone directly afterwards by setting
widthValid flag to false.
- In the case of the test case, the implicitWidth was already 120,
so geometryChanged is not emitted. However, when the height of the background
is set, this *does* cause itemGeometryChanged() to be called, which in turn
does the following:
extra.value().hasBackgroundWidth = p->widthValid;
extra.value().hasBackgroundHeight = p->heightValid;
resizeBackground();
So now all of the following checks evaluate to false:
(!p->widthValid || !extra.isAllocated() || !extra->hasBackgroundWidth)
This prevents the background from being resized.
This patch fixes the issue by unsetting the widthValid (and heightValid) flags
of the background directly after setting its width. To be safe, it also only
unsets it if we were the ones to set it. By doing this, the check mentioned
above succeeds because p->widthValid and extra->hasBackgroundWidth are both now
false.
It also adds some extra safety into itemGeometryChanged() that ensures that
extra.value().hasBackgroundWidth is only set if necessary, and therefore
prevents the extra data from unnecessarily being allocated. This is not
necessary for the fix, but feels like the right thing to do.
Change-Id: I051e281718bd8a2a20c100767d929fb71497ce1b
Fixes: QTBUG-71875
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Diffstat (limited to 'src/quicktemplates2/qquicktextfield.cpp')
-rw-r--r-- | src/quicktemplates2/qquicktextfield.cpp | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/src/quicktemplates2/qquicktextfield.cpp b/src/quicktemplates2/qquicktextfield.cpp index 15acfeb6..56ffc52f 100644 --- a/src/quicktemplates2/qquicktextfield.cpp +++ b/src/quicktemplates2/qquicktextfield.cpp @@ -184,13 +184,20 @@ void QQuickTextFieldPrivate::resizeBackground() QQuickItemPrivate *p = QQuickItemPrivate::get(background); if (((!p->widthValid || !extra.isAllocated() || !extra->hasBackgroundWidth) && qFuzzyIsNull(background->x())) || (extra.isAllocated() && (extra->hasLeftInset || extra->hasRightInset))) { + const bool wasWidthValid = p->widthValid; background->setX(getLeftInset()); background->setWidth(width - getLeftInset() - getRightInset()); + // If the user hadn't previously set the width, that shouldn't change when we set it for them. + if (!wasWidthValid) + p->widthValid = false; } if (((!p->heightValid || !extra.isAllocated() || !extra->hasBackgroundHeight) && qFuzzyIsNull(background->y())) || (extra.isAllocated() && (extra->hasTopInset || extra->hasBottomInset))) { + const bool wasHeightValid = p->heightValid; background->setY(getTopInset()); background->setHeight(height - getTopInset() - getBottomInset()); + if (!wasHeightValid) + p->heightValid = false; } resizingBackground = false; @@ -386,8 +393,13 @@ void QQuickTextFieldPrivate::itemGeometryChanged(QQuickItem *item, QQuickGeometr return; QQuickItemPrivate *p = QQuickItemPrivate::get(item); - extra.value().hasBackgroundWidth = p->widthValid; - extra.value().hasBackgroundHeight = p->heightValid; + // QTBUG-71875: only allocate the extra data if we have to. + // resizeBackground() relies on the value of extra.isAllocated() + // as part of its checks to see whether it should resize the background or not. + if (p->widthValid || extra.isAllocated()) + extra.value().hasBackgroundWidth = p->widthValid; + if (p->heightValid || extra.isAllocated()) + extra.value().hasBackgroundHeight = p->heightValid; resizeBackground(); } |