Buddy: search for a buddy bottom-up rather than top-down

As it stood, we would search for a label's buddy from the top level
widget and down, based on the object name. The problem with that approach
is that since the same form can be instantiated onto multiple widgets
inside the same window, there can exist multiple buddies with the same
name as well. As a result, the form builder would end up resolving the
same buddy instance for the all the form instances.

Rather than searching for the buddy top-down, this patch will instead
search for it bottom-up. We assume then, that the buddy closest to
the label is the correct one to use.

Fixes: QTBUG-96693
Pick-to: 6.5
Change-Id: Iae7c8865563304810ed5f905c6ad45d7b796b7a5
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
(cherry picked from commit 1c121ea7ab4791bf26759013c1a77eac9d76e3c3)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit e3cc2b1f8de7c31553cb728642dd06e690ef1d0c)
(cherry picked from commit 5895269bf765ff1f01f6fca22a15a6f5e04a9b4b)
This commit is contained in:
Richard Moe Gustavsen
2025-06-06 16:17:08 +00:00
committed by Qt Cherry-pick Bot
parent ee0feb5c14
commit 6ca4f67047
@@ -174,7 +174,18 @@ bool QFormBuilderExtra::applyBuddy(const QString &buddyName, BuddyMode applyMode
return false;
}
const QWidgetList widgets = label->topLevelWidget()->findChildren<QWidget*>(buddyName);
// QTBUG-96693: Because the same form can be instantiated on multiple
// widgets, when for example using a form for custom widgets, there can
// exist multiple widgets with the same buddy name in the window. Since
// the buddy closest to the label is the correct one to use, we search
// for the buddy bottom-up rather than top-down.
QWidgetList widgets;
QWidget *parent = label->parentWidget();
while (parent && widgets.isEmpty()) {
widgets = parent->findChildren<QWidget*>(buddyName);
parent = parent->parentWidget();
}
if (widgets.isEmpty()) {
label->setBuddy(nullptr);
return false;