VS+Qt 如何实现自定义菜单栏

功能界面

VS+Qt 如何实现自定义菜单栏_第1张图片

自定义菜单栏实现

mymenu.h定义槽函数:

private slots:

void on_actionOpen_triggered();
void on_actionSave_triggered();
void on_actionExit_triggered();
void on_actionMain_triggered();
void on_actionWarmUp_triggered();
void on_actionTest_triggered();
void on_actionRun_triggered();
void on_actionOptim_triggered();

mymenu.cpp中实现菜单栏自定义:

void MyMenu::iniMenuBar()
{
	QHBoxLayout* hlay = new QHBoxLayout(this);
	hlay->setMargin(0);
	QMenuBar *menuBar = new QMenuBar(this);
	//菜单文件
	QMenu* fileMenu = menuBar->addMenu(QStringLiteral("文件(&F)"));// QStringLiteral(
	QAction* actOpenFile = new QAction(QStringLiteral("打开文件(&O)"));
	QAction* actSaveFile = new QAction(QStringLiteral("保存(&S)"));
	QAction* actExit = new QAction(QStringLiteral("退出(&Q)"));
	connect(actOpenFile, SIGNAL(triggered()), this, SLOT(on_actionOpen_triggered()));
	connect(actSaveFile, SIGNAL(triggered()), this, SLOT(on_actionSave_triggered()));
	connect(actExit, SIGNAL(triggered()), this, SLOT(on_actionExit_triggered()));
	fileMenu->addAction(actOpenFile);
	fileMenu->addAction(actExit);

	// 菜单视图
	QMenu* backMenu = menuBar->addMenu(QStringLiteral("视图(&B)"));
	QAction* actBackParams = new QAction(QStringLiteral("主参数界面(&M)"));
	QAction* actionWarmUp = new QAction(QStringLiteral("暖机界面(&W)"));
	connect(actBackParams, SIGNAL(triggered()), this, SLOT(on_actionMain_triggered()));
	connect(actionWarmUp, SIGNAL(triggered()), this, SLOT(on_actionWarmUp_triggered()));
	backMenu->addAction(actBackParams);
	backMenu->addAction(actionWarmUp);

	// 菜单选项
	QMenu* optimMenu = menuBar->addMenu(QStringLiteral("选项(&P)"));
	QAction *actTest = new QAction(QStringLiteral("测试(&J)"));
	QAction *actRun = new QAction(QStringLiteral("运行(&S)"));
	QAction *actOptim = new QAction(QStringLiteral("优化(&Y)"));
	connect(actTest, SIGNAL(triggered()), this, SLOT(on_actionTest_triggered()));
	connect(actRun, SIGNAL(triggered()), this, SLOT(on_actionRun_triggered()));
	connect(actOptim, SIGNAL(triggered()), this, SLOT(on_actionOptim_triggered()));
	optimMenu->addAction(actTest);
	optimMenu->addAction(actRun);
	optimMenu->addAction(actOptim);

	hlay->addWidget(menuBar);

	ui.wgtMenuBar->setLayout(hlay);
	ui.wgtMenuBar->setVisible(true);
}

你可能感兴趣的:(Qt)