qt 在点击菜单下的动作之后获取该菜单的名称

问题:

QMenu 类下有4个信号:

void aboutToHide()
void aboutToShow()
void hovered(QAction *action)
void triggered(QAction *action)

关联信号 triggered(QAction *action) 后,要查询 该action 对应的菜单,应该如何操作呢?

解决过程:

1、首先关联信号和槽函数

  connect(childMenu,SIGNAL(triggered(QAction *)),this,SLOT(onchildMenu_Triggered(QAction *)));

手册中有提到menu()函数可以获取菜单项,于是 槽函数的内容中添加:

qDebug()<<“the action slot is here!”+_action->menu()->title();

来调试,具体如下:

void NavigationButton:: onchildMenu_Triggered(QAction *_action)
{
    qDebug()<<"the action slot is here!"+_action->text();

   qDebug()<<"the action slot is here!"+_action->menu()->title();

}

发现调试出错!!!!!!

于是,另找途径

2、通过获取发送者的指针,进而得到菜单名称

改写槽函数如下:

void NavigationButton:: onchildMenu_Triggered(QAction *_action)
{
    qDebug()<<"the action slot is here!"+_action->text();
	//获取发送者指针
    QMenu *childMenu=qobject_cast<QMenu *>(sender());
     qDebug()<<"childMenu->objectName()=="<<childMenu->title();

}

调试OK!!!!

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐