Maverick33
Maverick33

Reputation: 337

How to know parent menu of a given QAction?

I have a list of QActions, some are added to the top level menu and few are added to submenus of top level.

Is there any way to know parent menu name of each actions?

QAction *act;

I'm trying act->parentWidget(). But how can I get menu name from this?

Upvotes: 1

Views: 1692

Answers (1)

Cayan
Cayan

Reputation: 462

You can check if the result of act->parentWidget() if it is a valid pointer, if so you can manipulate as a normal widget.

To get the menu name, it depends on which widget you are using.

If is QMenu, you can retrieve the menu title via the title function.

QAction *act;
...
QWidget *widget = act->parentWidget();
if (widget) {
    QMenu *menu = dynamic_cast<QMenu*>(widget);
    menu->title();
}

Upvotes: 3

Related Questions