diff --git a/autotests/kxmlgui_unittest.h b/autotests/kxmlgui_unittest.h --- a/autotests/kxmlgui_unittest.h +++ b/autotests/kxmlgui_unittest.h @@ -47,6 +47,7 @@ void testClientDestruction(); void testMenusNoXmlFile(); void testShortcuts(); + void testPopupMenuParent(); }; #endif diff --git a/autotests/kxmlgui_unittest.cpp b/autotests/kxmlgui_unittest.cpp --- a/autotests/kxmlgui_unittest.cpp +++ b/autotests/kxmlgui_unittest.cpp @@ -1059,3 +1059,20 @@ factory.removeClient(&client); } + +void KXmlGui_UnitTest::testPopupMenuParent() +{ + const QByteArray xml = + "\n" + "\n" + "\n" + "Foo\n" + ""; + + TestXmlGuiWindow mainWindow(xml); + mainWindow.createGUI(); + + auto popupMenu = mainWindow.menuByName(QStringLiteral("foo")); + QVERIFY(popupMenu); + QCOMPARE(popupMenu->parent(), &mainWindow); +} diff --git a/src/kxmlguibuilder.cpp b/src/kxmlguibuilder.cpp --- a/src/kxmlguibuilder.cpp +++ b/src/kxmlguibuilder.cpp @@ -152,12 +152,14 @@ // use it as parent widget (to get kaction to plug itself into the // mainwindow). Don't use a popupmenu as parent widget, otherwise // the popup won't be hidden if it is used as a standalone menu as well. - // And we don't want to set the parent for a standalone popupmenu, - // otherwise its shortcuts appear. - // // Note: menus with a parent of 0, coming from child clients, can be // leaked if the child client is deleted without a proper removeClient call, though. QWidget *p = parent; + + if (!p && qobject_cast(d->m_widget)) { + p = d->m_widget; + } + while (p && !qobject_cast(p)) { p = p->parentWidget(); }