菜单栏基本操作

创建菜单栏

QMenuBar *menuBar = new QMenuBar(this); //1.创建菜单栏
menuBar->setGeometry(,,width(),); //设置大小 QMenu *fileMenu = new QMenu("File",this); //2.创建菜单
//3.创建行为(Action)
QAction *fileCreateAction = new QAction("create",this);
QAction *fileSaveAction = new QAction("save",this);
QAction *fileImportAction = new QAction("import",this);
QAction *fileExportAction = new QAction("export",this);
//4.将行为添加到菜单
fileMenu->addAction(fileSaveAction);
fileMenu->addAction(fileImportAction);
fileMenu->addAction(fileExportAction);
fileMenu->addAction(fileCreateAction);
//5.将菜单添加到菜单栏
menuBar->addMenu(fileMenu);

可以通过自定义槽函数实现想要的功能

//行为连接到具体的槽函数
connect(fileCreateAction,SIGNAL(triggered()),this,SLOT(createFile()));  //槽函数需要实现
connect(fileSaveAction,SIGNAL(triggered()),this,SLOT(saveFile()));
connect(fileImportAction,SIGNAL(triggered()),this,SLOT(importFile()));
connect(fileExportAction,SIGNAL(triggered()),this,SLOT(exportFile()));

为菜单再添加菜单实现多级菜单

QMenu *optionMenu = new QMenu("Option",this);   //创建菜单
QAction *optionSystemAction = new QAction("System",this);
QMenu *ComparisionMenu = new QMenu("Comparison",this);//**这里创建再一个菜单
QAction *optionFindEdgeAction = new QAction("Find edge",this);
QAction *optionSetDecimalAction = new QAction("Set decima",this);
optionMenu->addAction(optionSystemAction);
//添加二级菜单
optionMenu->addMenu(ComparisionMenu);//***
QAction *openAction = new QAction("open",this);
QAction *closeAction = new QAction("close",this);
//为二级菜单添加行为
ComparisionMenu->addAction(openAction);
ComparisionMenu->addAction(closeAction); optionMenu->addAction(optionFindEdgeAction);
optionMenu->addAction(optionSetDecimalAction);
menuBar->addMenu(optionMenu);

工具栏

//新建一个工具栏
QToolBar *toolBar = new QToolBar("toolbar",this);
//设置大小
toolBar->setGeometry(,,width(),); //新建行为(Action)
tAction1 = new QAction(QIcon(":/icon/icon/Vegetables-_11.png"),"Vegetables",this);
tAction2 = new QAction(QIcon(":/icon/icon/Vegetables-_12.png"),"Vegetables",this);
tAction3 = new QAction(QIcon(":/icon/icon/Vegetables-_13.png"),"Vegetables",this); //设置可选中,默认为false,triggered(bool)信号传递的bool永远为false
tAction1->setCheckable(true);
tAction2->setCheckable(true);
tAction3->setCheckable(true); //可以通过判断是否选中设置不同的样式
connect(tAction1,SIGNAL(triggered(bool)),this,SLOT(changeIcon(bool))); //添加行为
toolBar->addAction(tAction1);
toolBar->addAction(tAction2);
toolBar->addAction(tAction3);
void MainWindow::changeIcon(bool boolValue)
{
if(boolValue)
tAction1->setIcon(QIcon(":/icon/icon/Vegetables-_1.png"));
else
tAction1->setIcon(QIcon(":/icon/icon/Vegetables-_11.png"));
}

Qt之菜单栏工具栏入门的更多相关文章

  1. 《Qt Quick 4小时入门》学习笔记2

    http://edu.csdn.net/course/detail/1042/14805?auto_start=1   Qt Quick 4小时入门 第五章:Qt Quick基本界面元素介绍   1. ...

  2. 《Qt Quick 4小时入门》学习笔记4

    http://edu.csdn.net/course/detail/1042/14806?auto_start=1 Qt Quick 4小时入门 第七章:处理鼠标与键盘事件 1.处理鼠标事件 鼠标信号 ...

  3. 《Qt Quick 4小时入门》学习笔记3

    http://edu.csdn.net/course/detail/1042/14807?auto_start=1 Qt Quick 4小时入门 第八章:Qt Quick中的锚(anchors)布局 ...

  4. 《Qt Quick 4小时入门》学习笔记

    http://edu.csdn.net/course/detail/1042/14804?auto_start=1   Qt Quick 4小时入门 第五章:Qt Quick里的信号与槽   QML中 ...

  5. Robot Framework--02 菜单栏&工具栏

    转自:http://blog.csdn.net/tulituqi/article/details/7584795 我把RIDE的界面大致分了四个区域:菜单栏.工具栏.案例及资源区.工作区,如下图 菜单 ...

  6. Qt 从菜单栏打开文件

    Qt从菜单栏的下拉菜单选择文件 构造函数中设置打开动作信息 //打开文件 m_menu = ui.menu; // m_menu->menuAction = new QAction(QIcon( ...

  7. gvim设置字体和隐藏菜单栏工具栏

    liunx下面设置字体 set guifont=Monaco\ 注意空格的位置,其他写法不认哦! Windows下面设置 set guifont=Monaco:h 隐藏菜单栏 set guioptio ...

  8. qt之菜单栏的创建

    很久之前学习的Qt菜单栏的消息响应,昨天发现忘记了,今天又拿起来了,记一下笔记: 就像平常我们常用的软件一样,每个程序基本都有菜单栏,在菜单栏中有很多功能性的按钮,点击这些按钮会弹出有对应的菜单功能, ...

  9. PyQt4 菜单栏 + 工具栏 + 状态栏 + 中心部件 生成一个文本编辑部件示例

    我们将创建一个菜单栏.一个工具栏.一个状态栏和一个中心部件. #!/usr/bin/python # -*- coding:utf-8 -*- import sys from PyQt4 import ...

随机推荐

  1. 《Machine Learning Yearing》读书笔记

    ——深度学习的建模.调参思路整合. 写在前面 最近偶尔从师兄那里获取到了吴恩达教授的新书<Machine Learning Yearing>(手稿),该书主要分享了神经网络建模.训练.调节 ...

  2. 2019微软Power BI 每月功能更新系列——Power BI 4月版本功能完整解读

    Power BI4月份的更新对整个产品进行了重大更新.此版本增加了基于DAX表达式定义视觉效果标题和按钮URL的功能.本月Power BI也新增了许多新的连接器,现在可以使用几种预览连接器,包括Pow ...

  3. React中使用echarts

    1.安装相关的依赖: cnpm i react-for-echarts -S cnpm i echarts -S 2.使用方法: 页面引入: import ReactEcharts from 'ech ...

  4. 移动端调用ArcGIS Server 10.1服务

    1.最好用mdb数据源,不要用shp文件作为数据源,一是属性字段长度超过5个字符会被截断,二是中文会变成乱码. 2.mxd的layer的坐标系要是WGS1984(4362),不然属性查询不出来.

  5. UVa 712

    这个题根本不用建树,因为是完全二叉树,可以把这个想成二进制.对于根是二进制数的首位,之后依次类推.到最后的叶子节点就是从0到pow(2,n)-1. 关键在于在第一次输入的不是按照x1,x2,x3,x4 ...

  6. JavaScript基础之对象属性的检测和枚举

    属性检测 对象作为属性的集合,属性又包括自有属性和继承属性: 检测方法: \__   in运算符: \__ var obj = { x:1 } console.log( 'toString' in o ...

  7. 关于 API

    1. API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无需访 ...

  8. idea上手

    IntelliJ Idea 常用快捷键列表 最常用: Ctrl+P,可以显示参数信息 Alt+Insert,可以生成构造器/Getter/Setter等 Ctrl+Enter,导入包,自动修正 Ctr ...

  9. 2019OO第一单元作业总结

    OO第一单元作业的主题是求导,下面将分三次作业分别总结一下. --------------------------------------------------------------------- ...

  10. LinkedList浅析

    LinkedList浅析 LinkedList是Collection下的一个list实现,就像ArrayList一样. 和ArrayList不同的是它是链表结构,而ArrayList是顺序结构.我们平 ...