参考:QT多级菜单 - 知乎 (zhihu.com)
运行效果图:
关键代码:
void personMenu::contextMenuEvent(QContextMenuEvent *event)
{
//我完全可以写出一个代码生成器来把这些代码生成出来。
parent=new QMenu(this);
parent->addAction(QIcon(":/images/Menu/_消息.png"),"发送即时消息");
parent->addAction("发送电子邮件");
parent->addSeparator();
parent->addAction("查看资料");
parent->addAction("消息免打扰");
parent->addAction("分享他的名片");
parent->addAction(QIcon(":/images/Menu/时钟.png"),"消息记录");
parent->addSeparator();
QMenu * son1=parent->addMenu(QString("设置权限"));
son1->addAction("屏蔽此人消息");
son1->addAction("在线对其隐身");
parent->addAction(QIcon(":/images/Menu/编辑.png"),"修改好友备注");
QMenu * son2=parent->addMenu(QIcon(":/images/Menu/管理人员.png"),QString("移动联系人至"));
son2->addAction("黑名单");
// parent->addAction(QIcon(""),"");
// parent->addAction("");
parent->addAction(QIcon(":/images/Menu/删除.png"),"删除好友");
parent->addAction("举报此用户");
QMenu * son3=parent->addMenu(QIcon(":/images/Menu/管理人员.png"),"好友管理");
son3->addAction(QIcon(":/images/Menu/爱心.png"),"设置特别关心");
son3->addAction(QIcon(":/images/Menu/推荐人.png"),"给TA推荐好友");
son3->addAction("好友上线提醒");
son3->addAction(QIcon(":/images/Menu/社区管理_客户管理.png"),"好友管理器");
parent->addSeparator();
QMenu * son4=parent->addMenu(QIcon(":/images/Menu/快捷.png"),"会员快捷功能");
son4->addAction(QIcon(":/images/Menu/个性装扮.png"),"设置QQ装扮");
son4->addSeparator();
son4->addAction(QIcon(":/images/Menu/皇冠.png"),"会员群特权");
son4->addAction("群克隆");
son4->addAction("好友克隆");
son4->addAction("好友上线通知");
son4->addSeparator();
son4->addAction(QIcon(":/images/Menu/_会员卡.png"),"开通会员");
son4->addAction("索要会员");
parent->addAction(QIcon(":/images/Menu/形状-星形.png"),"进入QQ空间");
parent->setAttribute(Qt::WA_TranslucentBackground,true);
parent->setWindowOpacity(0.85);
son1->setAttribute(Qt::WA_TranslucentBackground,true);
son1->setWindowOpacity(0.85);
son2->setAttribute(Qt::WA_TranslucentBackground,true);
son2->setWindowOpacity(0.85);
son3->setAttribute(Qt::WA_TranslucentBackground,true);
son3->setWindowOpacity(0.85);
son4->setAttribute(Qt::WA_TranslucentBackground,true);
son4->setWindowOpacity(0.85);
parent->move(cursor().pos());
parent->show();
return QWidget::contextMenuEvent(event);
}
源码:
Menu/QMenu.zip · 开心/Qt与学习通页面 - 码云 - 开源中国 (gitee.com)