本文详细的介绍了QMenuBra QMenu QAction 菜单栏的各种操作,例如操作:新建界面、加入菜单项、加入子菜单、新建二级子菜单、QAction分隔符、pMenuBar、点击信号、触发信号、设置图标、设置禁用、设置提示语 等操作
本文作者原创,未经允许禁止转载。
QT QMenuBra QMenu QAction 菜单栏 操作详解目录
1新建界面
2 加入菜单项
3 加入子菜单
4 新建二级子菜单
5 QAction分隔符
6 pMenuBar
7 点击信号
8 触发信号
9 设置图标
10 设置禁用
11 设置提示语
QMenuBar: 就是所有窗口的菜单栏,在此基础上添加不同的QMenu和QAction
QMenu: 菜单栏里面菜单,可以显示文本和图标,但是并不负责执行操作,有点类似label的作用
QAction: Qt 将用户与界面进行交互的元素抽象为一种“动作”,使用QAction类表示。QAction才是真正负责执行操作的部件。
刚创建的界面有自带的QMenuBar所以不需要在次创建
/* 菜单项 */
QMenu *m_menuFile;
QMenu *m_menuEdit;
QMenu *m_menuView;
QMenu *m_menuProject;
QMenu *m_menuTest;
/* 菜单项 窗口获得焦点时按alt+n即可打开"文件"菜单项,(&)快捷方式 */
m_menuFile = new QMenu("文件(&F)");
m_menuEdit = new QMenu("编辑(&E)");
m_menuView = new QMenu("视图(&V)");
m_menuProject = new QMenu("项目(&P)");
m_menuTest = new QMenu("调试(&D)");
//菜单项添加调试子菜单
m_menuTest->addAction(m_actionWindow);
m_menuTest->addAction(m_actionGraphics);
m_menuTest->addAction(m_actionWindow_);
m_menuTest->addAction(m_actionPerformance);
/* 子菜单 */
// 文件子菜单
QAction *m_actionNewFile;
QAction *m_actionOpen;
QAction *m_actionInitial;
// 编辑子菜单
QAction *m_actionConversion;
QAction *m_actionFind;
QAction *m_actionWithdraw;
// 视图子菜单
QAction *m_actionViewPlan;
QAction *m_actionViewTeam;
QAction *m_actionViewService;
// 项目子菜单
QAction *m_actionTemp;
// 调试子菜单
QAction *m_actionWindow;
QAction *m_actionGraphics;
QAction *m_actionPerformance;
/* 定义子菜单 (&)为子菜单快捷键*/
//文件子菜单
m_actionNewFile = new QAction("新建(&N)");
m_actionOpen = new QAction("打开(&O)");
m_actionInitial = new QAction(QIcon(":/new/prefix1/resource/soccer_ball.ico"),"初始页(&E)");
//编辑子菜单
m_actionConversion = new QAction("转到(&G)");
m_actionFind = new QAction("查找&替换(&F)");
m_actionWithdraw = new QAction("撤销(&U)");
//视图子菜单
m_actionViewPlan = new QAction("解决方案资源管理器(&P)");
m_actionViewTeam = new QAction("团队资源管理器(&M)");
m_actionViewService = new QAction("服务器资源管理器(&V)");
//项目子菜单
m_actionTemp = new QAction("导出模板(&E)");
//调试子菜单
m_actionWindow = new QAction("窗口(&W)");
m_actionGraphics = new QAction("图形(&C)");
m_actionWindow_ = new QAction();
m_actionPerformance = new QAction("性能探查器(&F)");
//菜单项添加文件子菜单
m_menuFile->addAction(m_actionNewFile);
m_menuFile->addAction(m_actionOpen);
m_menuFile->addAction(m_actionInitial);
//菜单项添加编辑子菜单
m_menuEdit->addAction(m_actionConversion);
m_menuEdit->addAction(m_actionFind);
m_menuEdit->addAction(m_actionWithdraw);
//菜单项添加视图子菜单
m_menuView->addAction(m_actionViewPlan);
m_menuView->addAction(m_actionViewTeam);
m_menuView->addAction(m_actionViewService);
//菜单项添加项目子菜单
m_menuProject->addAction(m_actionTemp);
//菜单项添加调试子菜单
m_menuTest->addAction(m_actionWindow);
m_menuTest->addAction(m_actionGraphics);
m_menuTest->addAction(m_actionWindow_);
m_menuTest->addAction(m_actionPerformance);
//二级子菜单
QMenu * m_menu_new;
QMenu * m_menu_open;
QMenu * m_menu_conversion;
QMenu * m_menu_find;
QMenu * m_menu_window;
QMenu * m_menu_graphics;
m_menu_new = new QMenu();
m_menu_open = new QMenu();
m_menu_conversion = new QMenu();
m_menu_find = new QMenu();
m_menu_window = new QMenu();
m_menu_graphics = new QMenu();
/** 编辑子菜单 二级菜单菜单子项 */
QAction *m_menu_conversion_con = new QAction(m_menu_conversion);
QAction *m_menu_conversion_all = new QAction(m_menu_conversion);
QAction *m_menu_conversion_file = new QAction(m_menu_conversion);
QAction *m_menu_conversion_ufile = new QAction(m_menu_conversion);
QAction *m_menu_conversion_type = new QAction(m_menu_conversion);
QAction *m_menu_conversion_members = new QAction(m_menu_conversion);
QAction *m_menu_conversion_symbol = new QAction(m_menu_conversion);
QAction *m_menu_conversion_back = new QAction(m_menu_conversion);
QAction *m_menu_find_f = new QAction(m_menu_find);
QAction *m_menu_find_r = new QAction(m_menu_find);
QAction *m_menu_find_l = new QAction(m_menu_find);
QAction *m_menu_find_s = new QAction(m_menu_find);
m_menu_conversion_con->setText("转到(&G)...");
m_menu_conversion_all->setText("转到所有(&G)...");
m_menu_conversion_file->setText("转到文件(&F)...");
m_menu_conversion_ufile->setText("转到最近的文件(&R)...");
m_menu_conversion_type->setText("转到类型(&T)...");
m_menu_conversion_members->setText("转到成员(&M)...");
m_menu_conversion_symbol->setText("转到符号(&S)...");
m_menu_conversion_back->setText("转到上一个编辑位置...");
m_menu_find_f->setText("快速查找(&F)...");
m_menu_find_r->setText("快速替换(&R)...");
m_menu_find_l->setText("在文件中查找(&I)...");
m_menu_find_s->setText("在文件中替换(&S)...");
m_menu_conversion->addAction(m_menu_conversion_con);
m_menu_conversion->addAction(m_menu_conversion_all);
m_menu_conversion->addAction(m_menu_conversion_file);
m_menu_conversion->addAction(m_menu_conversion_ufile);
m_menu_conversion->addAction(m_menu_conversion_type);
m_menu_conversion->addAction(m_menu_conversion_members);
m_menu_conversion->addAction(m_menu_conversion_symbol);
m_menu_conversion->addAction(m_menu_conversion_back);
m_menu_find->addAction(m_menu_find_f);
m_menu_find->addAction(m_menu_find_r);
m_menu_find->addAction(m_menu_find_l);
m_menu_find->addAction(m_menu_find_s);
/** 调试子菜单 二级菜单菜单子项 */
QAction *m_menu_window_T = new QAction(m_menu_window);
QAction *m_menu_graphics_I = new QAction(m_menu_graphics);
QAction *m_menu_graphics_X = new QAction(m_menu_graphics);
m_menu_window_T->setText("显示诊断工具(&T)...");
m_menu_graphics_I->setText("对安装的应用启动图形调试(&I)...");
m_menu_graphics_X->setText("DirectX控制面板(&X)...");
m_menu_window->addAction(m_menu_window_T);
m_menu_graphics->addAction(m_menu_graphics_I);
m_menu_graphics->addAction(m_menu_graphics_X);
//设置子菜单 m_actionGraphics
m_actionWindow->setMenu(m_menu_window);
m_actionGraphics->setMenu(m_menu_graphics);
菜单栏加入一个QAction将QAction的属性设置为true则可以将菜单项改变为分隔符
//在菜单项之间添加分割线
m_actionWindow_->setSeparator(true);
pMenuBar 代码创建菜单栏 (当不使用QMainWindow自带的菜单栏时,必须要加上此行)
//setMenuBar(pMenuBar);
private slots:
void s_menuPro();
void s_menuFile();
connect(m_menu_new_pro,SIGNAL(triggered()),SLOT(s_menuPro()));
connect(m_menu_new_file,SIGNAL(triggered()),SLOT(s_menuFile()));
void MainWindow::s_menuPro()
{
qDebug() << "新建项目 槽函数触发";
}
void MainWindow::s_menuFile()
{
qDebug() << "新建文件 槽函数触发";
}
private slots:
void s_menu_window_t();
connect(m_menu_window_T,SIGNAL(hovered()),SLOT(s_menu_window_t()));
void MainWindow::s_menu_window_t()
{
qDebug() << "诊断工具 鼠标触发 槽函数触发";
}
m_actionNewFile->setIcon(QIcon("D:/ico.png"));
//另外写法
//m_actionNewFile->setIcon(QIcon("D:/ico.png"),"setText");
m_actionNewFile->setEnabled(false);
m_actionNewFile->setStatusTip("提示语");