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

我的未来我决定 提交于 2020-03-07 04:39:14

功能界面

在这里插入图片描述

自定义菜单栏实现

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);
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!