QMenu 设置菜单图标 & 生成菜单树
效果图
源码
.h 文件
protected slots:
void onMenuTriggered(QAction*);
.cpp 文件
// 菜单
QMenu *pMenu = new QMenu(this);
pMenu->addAction(QString::fromLocal8Bit("我"));
pMenu->addSeparator();
pMenu->addAction(QString::fromLocal8Bit("爱"));
QMenu *pChildMenu = new QMenu(this);
pChildMenu->setTitle(QStringLiteral("学"));
pChildMenu->addAction(QStringLiteral("Q"));
pMenu->addMenu(pChildMenu);
QAction *act1 = new QAction(pChildMenu);
act1->setText(QStringLiteral("t"));
act1->setIcon(QIcon("src/Me.png"));
pChildMenu->addAction(act1);
QPushButton *pButton = new QPushButton(this);
// 按钮
pButton->setText(QString::fromLocal8Bit("主菜单"));
// 设置菜单
pButton->setMenu(pMenu);
connect(pMenu, SIGNAL(triggered(QAction*)), this, SLOT(onMenuTriggered(QAction*)));
void MyClass::onMenuTriggered(QAction *action)
{
QString str = action->text();
}