Qt QMenu之创建以及响应事件

QMenu* menu= new QMenu(); 
			QAction* Action1 = new QAction(menu);
			QAction* Action2 = new QAction(menu);
			QAction* Action3 = new QAction(menu);

			Action1->setText("IDCS_INTELLIGENT_ANALYSIS_WELCOME_GUEST");
			Action2->setText("IDCS_FACE_ATTEND");
			Action3->setText("IDCS_INTELLIGENT_ANALYSIS");

			menu->addAction(Action1);
			menu->addAction(Action2);
			menu->addAction(Action3);
			menu->setStyleSheet("QMenu {margin:0px;border: 1px solid rgb(0,0,0);} \
								QMenu::item {height:30px;color: rgb(255,255,255);padding: 0px 20px 0px 20px;background-color: rgb(0,0,0);} \
								QMenu::item:selected {color: rgb(97,96,96);background-color: rgb(232,188,7); } \
								QMenu::item:pressed {background-color: rgb(160,205,220);}\
								QMenu::separator {height: 1px;background: rgb(232,188,7);}");

			m_btnDetail->setMenu(menu);
			connect(Action1, SIGNAL(triggered()), this, SLOT(slot_ClickMenu1()));
			connect(Action2, SIGNAL(triggered()), this, SLOT(slot_ClickMenu2()));
			connect(Action3, SIGNAL(triggered()), this, SLOT(slot_ClickMenu3()));

 

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt中,右键菜单可以通过QMenu和QAction来实现。要让子菜单能够响应槽函数,可以使用QAction的triggered信号和槽函数进行连接。 首先,在Qt Designer中创建一个QMenu,并添加一些QAction作为子菜单。然后,通过右键菜单策略(例如在QWidget上设置setContextMenuPolicy为Qt::CustomContextMenu)将菜单与QWidget相关联。 接下来,在QWidget的构造函数中,创建QMenu和QAction,并将它们添加到QMenu中。然后,使用connect()函数将QAction的triggered信号与槽函数进行连接。槽函数的实现可以根据需要进行自定义。 以下是示例代码: ```cpp // 创建菜单和子菜单 QMenu* menu = new QMenu(this); QAction* action1 = new QAction("子菜单项1", this); QAction* action2 = new QAction("子菜单项2", this); // 将子菜单添加到菜单中 menu->addAction(action1); menu->addAction(action2); // 设置右键菜单策略 this->setContextMenuPolicy(Qt::CustomContextMenu); // 创建右键菜单动作 QAction* menuAction = new QAction("右键菜单", this); connect(menuAction, &QAction::triggered, this, &MyWidget::showContextMenu); // 将菜单动作添加到QWidget中 this->addAction(menuAction); // 显示槽函数 void MyWidget::showContextMenu() { QPoint cursorPos = QCursor::pos(); menu->exec(cursorPos); } // QAction的triggered信号连接的槽函数 void MyWidget::onActionTriggered() { // 处理子菜单项的点击事件 } ``` 在这个示例中,当用户右键单击QWidget时,会显示一个右键菜单。当用户单击子菜单项时,将会调用onActionTriggered()槽函数进行处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值