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 <lars.knoll@qt.io>
bb10
Volker Hilsheimer 2021-10-25 14:27:00 +02:00
parent 6ab16d52d2
commit 35ddf34988
3 changed files with 13 additions and 1 deletions

View File

@ -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.

View File

@ -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

View File

@ -189,6 +189,8 @@ public:
QAction *actionAt(const QPoint &) const;
QAction *menuAction() const;
static QMenu *menuInAction(const QAction *action)
{ return qobject_cast<QMenu *>(action->menuObject()); }
QString title() const;
void setTitle(const QString &title);