问题
How to get parent menu of a given QAction? I have a QActions added to submenus.
Is there any way to know parent menu name of each action?
ui->action567->parent() //return MainWindow
ui->action567->parentWidget() //return MainWindow
ui->action567->menu() //return nullptr.
ui->action567->actionGroup() //return nullptr.
I can get parent menu this way:
for( QMenu * menu : ui->menuBar->findChildren< QMenu * >() )
{
if( menu->actions().contains( ui->action567 ) )
{
qDebug() << menu << ui->action567 ;
break;
}
}
Does better and more native way exists?
回答1:
associatedWidgets() will return you a list of widgets this action was added to.
ui->action567->associatedWidgets();
来源:https://stackoverflow.com/questions/32006039/qaction-parent-menu