From 6ca4f670477ed5c46356762abaf180a2483db215 Mon Sep 17 00:00:00 2001 From: Richard Moe Gustavsen Date: Wed, 4 Jun 2025 14:03:00 +0200 Subject: [PATCH] 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 (cherry picked from commit 1c121ea7ab4791bf26759013c1a77eac9d76e3c3) Reviewed-by: Qt Cherry-pick Bot (cherry picked from commit e3cc2b1f8de7c31553cb728642dd06e690ef1d0c) (cherry picked from commit 5895269bf765ff1f01f6fca22a15a6f5e04a9b4b) --- src/designer/src/lib/uilib/formbuilderextra.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/designer/src/lib/uilib/formbuilderextra.cpp b/src/designer/src/lib/uilib/formbuilderextra.cpp index 5d5a14acf..0507573e3 100644 --- a/src/designer/src/lib/uilib/formbuilderextra.cpp +++ b/src/designer/src/lib/uilib/formbuilderextra.cpp @@ -174,7 +174,18 @@ bool QFormBuilderExtra::applyBuddy(const QString &buddyName, BuddyMode applyMode return false; } - const QWidgetList widgets = label->topLevelWidget()->findChildren(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(buddyName); + parent = parent->parentWidget(); + } + if (widgets.isEmpty()) { label->setBuddy(nullptr); return false;