目的:创建一级菜单栏,添加对应选项,并向一级菜单栏中添加二级菜单栏选项。
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//一级菜单
QMenu *menu1 = new QMenu(QString::fromUtf8("编辑"));
menu1->addAction(QString::fromUtf8("退出"));
menu1->addAction(QString::fromUtf8("工具栏"));
menu1->addAction(QString::fromUtf8("选项"));
menu1->addAction(QString::fromUtf8("信息"));
//二级菜单
QMenu *menu2 = new QMenu(QString::fromUtf8("保存为"));
menu2->addAction(QString::fromUtf8("保存为图片"));
menu2->addAction(QString::fromUtf8("保存为视频"));
menu2->addAction(QString::fromUtf8("保存为文本文件"));
//将二级菜单插入一级菜单栏中
menu1->addMenu(menu2);
//初始化为当前页面菜单栏
this->menuBar()->addMenu(menu1);
}
MainWindow::~MainWindow()
{
delete ui;
}
效果展示: