QMenu
创建
QMenu menu1;
//添加动作
//添加动作
QAction* act1=menu1.addAction(codec->toUnicode("动作1"));
QAction* act2=menu1.addAction(codec->toUnicode("动作2"));
QAction连接槽函数
//QAction连接槽函数 connect(act1, &QAction::triggered, this, [=]()
{ QMessageBox::information(this, "tip", u8"动作1事件"); }); connect(act2, &QAction::triggered, this, &QtWidgetsApplication2::MenuAction2); public slots: //事件2的槽函数定义
void MenuAction2(); /// <summary> /// 事件2的槽函数 /// </summary> void QtWidgetsApplication2::MenuAction2() { QMessageBox::information(this, "tip", u8"动作2事件"); };
//在菜单中添加一个分隔符
//在菜单中添加一个分隔符 menu1.addSeparator();
//在指定位置显示菜单作为弹出菜单,并返回用户选择的动作的指针
//在指定位置显示菜单作为弹出菜单,并返回用户选择的动作的指针 QAction* selectAction= menu1.exec(QCursor::pos()); if (selectAction)//有选择菜单的选项 { QString selectText = selectAction->text(); QMessageBox::information(this, "tip", selectText); } else//没有选择菜单 { QMessageBox::information(this, "tip", u8"没选择"); }
右键菜单
//打开右键菜单属性 ui.tableView->setContextMenuPolicy(Qt::CustomContextMenu); //右键菜单 QMenu* menu = new QMenu(ui.tableView); menu->addAction(u8"添加"); menu->addAction(u8"删除"); //响应右键菜单信号槽 connect(ui.tableView, &QTableView::customContextMenuRequested, this, &QtWidgetsApplication4::slotCustomContextMenuRequested);

浙公网安备 33010602011771号