diff --git a/src/kcollapsiblegroupbox.cpp b/src/kcollapsiblegroupbox.cpp --- a/src/kcollapsiblegroupbox.cpp +++ b/src/kcollapsiblegroupbox.cpp @@ -20,6 +20,7 @@ #include "kcollapsiblegroupbox.h" +#include #include #include #include @@ -297,6 +298,17 @@ void KCollapsibleGroupBox::overrideFocusPolicyOf(QWidget *widget) { + // https://bugs.kde.org/show_bug.cgi?id=396450 + // A label with word-wrapping enabled will break positioning of the groupbox in the layout. + // The cause seems to be the setFocusPolicy() call below, but it's not clear why. + // Until a proper fix is found, as workaround we toggle twice the groupbox which fixes the issue. + if (auto label = qobject_cast(widget)) { + if (label->wordWrap()) { + toggle(); + toggle(); + } + } + d->focusMap.insert(widget, widget->focusPolicy()); if (!isExpanded()) { diff --git a/tests/kcollapsiblegroupboxtest.cpp b/tests/kcollapsiblegroupboxtest.cpp --- a/tests/kcollapsiblegroupboxtest.cpp +++ b/tests/kcollapsiblegroupboxtest.cpp @@ -52,6 +52,12 @@ auto checkBox = new QCheckBox(QStringLiteral("Some really long text that goes on and on and on for ever and ever")); innerLayout->addWidget(checkBox); + auto label = new QLabel(groupBox); + label->setText(QStringLiteral("Some input field:")); + // Word-wrapping in labels triggers a bug in the layout positioning. + label->setWordWrap(true); + innerLayout->addWidget(label); + auto lineEdit = new QLineEdit(groupBox); innerLayout->addWidget(lineEdit);