如何知道给定QAction的父菜单? [英] How to know parent menu of a given QAction?

查看:115
本文介绍了如何知道给定QAction的父菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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;

我正在尝试 act-> parentWidget().但是如何从中获得菜单名称?

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

推荐答案

您可以检查 act-> parentWidget()的结果是否为有效指针,如果可以,则可以进行操作作为普通的小部件.

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.

如果是QMenu,则可以通过标题功能检索菜单标题.

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();
}

这篇关于如何知道给定QAction的父菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆