From 38dac54f6ad70ec465340845741e5fef804b9bde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20De=20Canni=C3=A8re?= Date: Wed, 17 Apr 2024 13:48:20 +0200 Subject: [PATCH] Controls: Bypass bindings when setting x and y in resizeBackground Using setX() and setY() instead can remove the binding for them. Fixes: QTBUG-120033 Change-Id: I77fc5360b2d10436b5e258b5f0ceb96b949eccbe Reviewed-by: Ulf Hermann (cherry picked from commit bb7ba7667b4cf3565aa1849d08cc71b9ac011e77) Reviewed-by: Qt Cherry-pick Bot (cherry picked from commit ac17f948cdb46e0fe900c72af35fc3ac84e29000) (cherry picked from commit 118f9999fe6438261faa697905aa89b1557c3774) Reviewed-by: Qt CI Bot (cherry picked from commit 3f9cfda16910d17f2a4c2372a8a50b89384cbc40) --- src/quicktemplates2/qquickcontrol.cpp | 14 +++++++-- .../controls/data/tst_control.qml | 30 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/quicktemplates2/qquickcontrol.cpp b/src/quicktemplates2/qquickcontrol.cpp index de3fd490ff..149e7b973d 100644 --- a/src/quicktemplates2/qquickcontrol.cpp +++ b/src/quicktemplates2/qquickcontrol.cpp @@ -385,12 +385,22 @@ void QQuickControlPrivate::resizeBackground() bool changeHeight = false; if (((!p->widthValid() || !extra.isAllocated() || !extra->hasBackgroundWidth) && qFuzzyIsNull(background->x())) || (extra.isAllocated() && (extra->hasLeftInset || extra->hasRightInset))) { - background->setX(getLeftInset()); + const auto leftInset = getLeftInset(); + if (!qt_is_nan(leftInset) && p->x.valueBypassingBindings() != leftInset) { + // We bypass the binding here to prevent it from being removed + p->x.setValueBypassingBindings(leftInset); + p->dirty(DirtyType::Position); + } changeWidth = !p->width.hasBinding(); } if (((!p->heightValid() || !extra.isAllocated() || !extra->hasBackgroundHeight) && qFuzzyIsNull(background->y())) || (extra.isAllocated() && (extra->hasTopInset || extra->hasBottomInset))) { - background->setY(getTopInset()); + const auto topInset = getTopInset(); + if (!qt_is_nan(topInset) && p->y.valueBypassingBindings() != topInset) { + // We bypass the binding here to prevent it from being removed + p->y.setValueBypassingBindings(topInset); + p->dirty(DirtyType::Position); + } changeHeight = !p->height.hasBinding(); } if (changeHeight || changeWidth) { diff --git a/tests/auto/quickcontrols2/controls/data/tst_control.qml b/tests/auto/quickcontrols2/controls/data/tst_control.qml index e12cbbf4ef..5103d9afd4 100644 --- a/tests/auto/quickcontrols2/controls/data/tst_control.qml +++ b/tests/auto/quickcontrols2/controls/data/tst_control.qml @@ -502,6 +502,36 @@ TestCase { verify(control.background.height !== control.height) } + Component { + id: backgroundTest2 + Button { + id: btn + width: 100 + height: 100 + topInset: 0 + objectName: "" + + background: Rectangle { + id: bg + implicitHeight: 80 + border.color: "red" + y: btn.objectName === "aaa" ? 20 : 0 + } + } + } + + // QTBUG-120033: Make sure that the binding for y on the tab button's background doesn't get removed + function test_background2() { + let button = createTemporaryObject(backgroundTest2, testCase) + verify(button) + + verify(button.background.y === 0) + button.objectName = "aaa" + verify(button.background.y === 20) + button.objectName = "" + verify(button.background.y === 0) + } + Component { id: component2 T.Control {