diff --git a/src/kstatusnotifieritem.h b/src/kstatusnotifieritem.h --- a/src/kstatusnotifieritem.h +++ b/src/kstatusnotifieritem.h @@ -359,10 +359,13 @@ * usually you don't need to call this unless you want to use * a custom QMenu subclass as context menu. * - * The KStatusNotifierItem instance takes ownership of the menu, - * and will delete it upon its destruction. + * By default, the KStatusNotifierItem instance takes ownership + * of the menu, and will delete it upon its destruction. + * @param menu new context menu for this StatusNotifierItem + * @param takeOwnership if it's true, KStatusNotifierItem will take + * the ownership */ - void setContextMenu(QMenu *menu); + void setContextMenu(QMenu *menu, bool takeOwnership = true); /** * Access the context menu associated to this status notifier item diff --git a/src/kstatusnotifieritem.cpp b/src/kstatusnotifieritem.cpp --- a/src/kstatusnotifieritem.cpp +++ b/src/kstatusnotifieritem.cpp @@ -70,7 +70,7 @@ delete d->statusNotifierWatcher; delete d->notificationsClient; delete d->systemTrayIcon; - if (!qApp->closingDown()) { + if (!qApp->closingDown() && d->menuOwnership) { delete d->menu; } if (d->associatedWidget) { @@ -416,7 +416,7 @@ return d->toolTipSubTitle; } -void KStatusNotifierItem::setContextMenu(QMenu *menu) +void KStatusNotifierItem::setContextMenu(QMenu *menu, bool takeOwnership) { if (d->menu && d->menu != menu) { d->menu->removeEventFilter(this); @@ -449,6 +449,7 @@ } d->menu = menu; + d->menuOwnership = takeOwnership; Qt::WindowFlags oldFlags = d->menu->windowFlags(); d->menu->setParent(nullptr); d->menu->setWindowFlags(oldFlags); @@ -764,7 +765,8 @@ systemTrayIcon(nullptr), hasQuit(false), onAllDesktops(false), - standardActionsEnabled(true) + standardActionsEnabled(true), + menuOwnership(true) { } diff --git a/src/kstatusnotifieritemprivate_p.h b/src/kstatusnotifieritemprivate_p.h --- a/src/kstatusnotifieritemprivate_p.h +++ b/src/kstatusnotifieritemprivate_p.h @@ -155,7 +155,7 @@ QString iconThemePath; QString menuObjectPath; - QMenu *menu; + QPointer menu; QHash actionCollection; QWidget *associatedWidget; QPoint associatedWidgetPos; @@ -173,6 +173,7 @@ bool hasQuit : 1; bool onAllDesktops : 1; bool standardActionsEnabled : 1; + bool menuOwnership : 1; }; #endif