From 35ddf3498859dc184456346f2b070fe94dabaf26 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Mon, 25 Oct 2021 14:27:00 +0200 Subject: [PATCH] Add QMenu::menuInAction as a static helper QAction::menu is deprecated, as it makes QAction (a QtGui class) depend on QtWidgets. The template hack works, but shouldn't become a permanent solution and is already deprecated. To get the QMenu out of a QAction that contains it, add a static helper to QMenu instead. QAction continues to store the menu pointer so that we don't have to use a dynamic property or an associative container in QMenu. Change-Id: Ieb6a2b1900d2fc2f16dd5a4a8ab7da98604642d3 Reviewed-by: Lars Knoll --- src/gui/kernel/qaction.cpp | 2 +- src/widgets/widgets/qmenu.cpp | 10 ++++++++++ src/widgets/widgets/qmenu.h | 2 ++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/gui/kernel/qaction.cpp b/src/gui/kernel/qaction.cpp index 9c0b1613dc..2d8d289c98 100644 --- a/src/gui/kernel/qaction.cpp +++ b/src/gui/kernel/qaction.cpp @@ -1225,7 +1225,7 @@ QAction::MenuRole QAction::menuRole() const /*! \fn QMenu *QAction::menu() const - \deprecated + \deprecated Use QMenu::menuForAction instead. Returns the menu contained by this action. diff --git a/src/widgets/widgets/qmenu.cpp b/src/widgets/widgets/qmenu.cpp index 74ac7e799c..5ecac2c7c6 100644 --- a/src/widgets/widgets/qmenu.cpp +++ b/src/widgets/widgets/qmenu.cpp @@ -1052,6 +1052,16 @@ QAction *QMenu::menuAction() const return d_func()->menuAction; } +/*! + \fn static QMenu *QMenu::menuInAction(const QAction *action) + + Returns the menu contained by \a action, or \nullptr if \a action does not + contain a menu. + + In widget applications, actions that contain menus can be used to create menu + items with submenus, or inserted into toolbars to create buttons with popup menus. +*/ + /*! \property QMenu::title \brief The title of the menu diff --git a/src/widgets/widgets/qmenu.h b/src/widgets/widgets/qmenu.h index 68481308fb..1b14263e6b 100644 --- a/src/widgets/widgets/qmenu.h +++ b/src/widgets/widgets/qmenu.h @@ -189,6 +189,8 @@ public: QAction *actionAt(const QPoint &) const; QAction *menuAction() const; + static QMenu *menuInAction(const QAction *action) + { return qobject_cast(action->menuObject()); } QString title() const; void setTitle(const QString &title);