1. C++ GUI Qt4编程第三章,添加menu菜单。

2. mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow> class QMenu;
class QAction; class MainWindow : public QMainWindow
{
Q_OBJECT public:
MainWindow(); private:
/*菜单*/
QMenu *fileMenu;
QMenu *editMenu;
QMenu *toolsMenu;
QMenu *optionsMenu;
QMenu *helpMenu; /*File动作*/
QAction *newAction;
QAction *openAction;
QAction *saveAction;
QAction *saveAsAction;
QAction *exitAction; /*Edit动作*/
QAction *cutAction;
QAction *copyAction;
QAction *pasteAction;
QAction *deleteAction;
QMenu *selectSubMenu;
QAction *selectRowAction;
QAction *selectColumnAction;
QAction *selectAllAction;
QAction *findAction;
QAction *goToCellAction; /*Tools动作*/
QAction *recalculateAction;
QAction *sortAction; /*Options动作*/
QAction *showGridAction;
QAction *autoRecalculateAction; /*Help动作*/
QAction *aboutAction;
QAction *aboutQtAction; void createMenus();
void createActions();
void createFileActions();
void createEditActions();
void createToolsActions();
void createOptionsActions();
void createHelpAction();
}; #endif /*MAINWINDOW_H*/

3. mainwindow.cpp

#include <QMenu>
#include <QMenuBar>
#include <QAction>
#include "mainwindow.h" MainWindow::MainWindow()
{
createActions();
createMenus();
} void MainWindow::createMenus()
{
/*file menu*/
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAction);
fileMenu->addAction(openAction);
fileMenu->addAction(saveAction);
fileMenu->addAction(saveAsAction);
fileMenu->addSeparator();
fileMenu->addAction(exitAction); /*edit menu*/
editMenu = menuBar()->addMenu(tr("&Edit"));
editMenu->addAction(cutAction);
editMenu->addAction(copyAction);
editMenu->addAction(pasteAction);
editMenu->addAction(deleteAction);
selectSubMenu = editMenu->addMenu(tr("&Select"));
selectSubMenu->addAction(selectRowAction);
selectSubMenu->addAction(selectColumnAction);
selectSubMenu->addAction(selectAllAction);
editMenu->addSeparator();
editMenu->addAction(findAction);
editMenu->addAction(goToCellAction); /*tools menu*/
toolsMenu = menuBar()->addMenu(tr("&Tools"));
toolsMenu->addAction(recalculateAction);
toolsMenu->addAction(sortAction); /*option menu*/
optionsMenu = menuBar()->addMenu(tr("&Option"));
optionsMenu->addAction(showGridAction);
optionsMenu->addAction(autoRecalculateAction); /*间隔器*/
menuBar()->addSeparator(); /*help menu*/
helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutAction);
helpMenu->addAction(aboutQtAction);
} void MainWindow::createActions()
{
createFileActions();
createEditActions();
createToolsActions();
createOptionsActions();
createHelpAction();
} /*file动作*/
void MainWindow::createFileActions()
{
newAction = new QAction(tr("&New"), this);
openAction = new QAction(tr("&Open"), this);
saveAction = new QAction(tr("&Save"), this);
saveAsAction = new QAction(tr("Save &As..."), this);
exitAction = new QAction(tr("E&xit"), this);
} /*edit动作*/
void MainWindow::createEditActions()
{
cutAction = new QAction(tr("Cu&t"), this);
copyAction = new QAction(tr("&Copy"), this);
pasteAction = new QAction(tr("&Paste"), this);
deleteAction = new QAction(tr("&Delete"), this);
selectRowAction = new QAction(tr("&Row"), this);
selectColumnAction = new QAction(tr("&Column"), this);
selectAllAction = new QAction(tr("&All"), this);
findAction = new QAction(tr("&Find..."), this);
goToCellAction = new QAction(tr("&Go to Cell..."), this);
} /*tools动作*/
void MainWindow::createToolsActions()
{
recalculateAction = new QAction(tr("&Recalculate"), this);
sortAction = new QAction(tr("&Sort..."), this);
} /*options动作*/
void MainWindow::createOptionsActions()
{
showGridAction = new QAction(tr("&Show Grid"), this);
autoRecalculateAction = new QAction(tr("Auto-Recalculate"), this);
} /*help动作*/
void MainWindow::createHelpAction()
{
aboutAction = new QAction(tr("&About"), this);
aboutQtAction = new QAction(tr("About &Qt"), this);
}

4. main.cpp

#include <QApplication>
#include "mainwindow.h" int main(int argc, char *argv[])
{
QApplication app(argc, argv); MainWindow *win = new MainWindow;
win->show(); return app.exec();
}

C++ GUI Qt4编程(07)-3.1menu的更多相关文章

  1. C++ GUI Qt4编程(10)-3.4spreadsheet

    1. C++ GUI Qt4编程第三章,增加spreadsheet. 2. spreadsheet.h /**/ #ifndef SPREADSHEET_H #define SPREADSHEET_H ...

  2. C++ GUI Qt4编程(09)-3.3spreadsheet-toolbar

    1. C++ GUI Qt4编程第三章,增加工具栏.状态栏和快捷键. 2. mainwindow.h /**/ #ifndef MAINWINDOW_H #define MAINWINDOW_H #i ...

  3. C++ GUI Qt4编程(08)-3.2spreadsheet-resource

    1. C++ GUI Qt4编程第三章,图片使用资源机制法. 2. 步骤: 2-1. 在resource文件夹下,新建images文件,存放图片. 2-2. 新建spreadsheet.qrc文件,并 ...

  4. C++ GUI Qt4编程(03)-1.3layout

    1. 根据C++ GUI Qt4编程(第二版)整理2. 系统:centos7:  Qt版本:5.5.13. 程序:layout.cpp #include <QApplication> #i ...

  5. C++ GUI Qt4编程(02)-1.2quit

    1. 根据C++ GUI Qt4编程(第二版)整理2. 系统:centos7:  Qt版本:5.5.13. 程序:quit.cpp #include <QApplication> #inc ...

  6. C++ GUI Qt4编程(01)-1.1Hello Qt

    1. 根据C++ GUI Qt4编程(第二版)整理2. 系统:centos7:  Qt版本:5.5.13. 程序:hello.cpp #include <QApplication> #in ...

  7. C++ GUI Qt4编程-创建自定义窗口部件

    C++ GUI Qt4编程-创建自定义窗口部件   Qtqt4 通过Qt窗口部件进行子类化或者直接对QWidget进行子类化,就可以创建自定义窗口部件,下面示范两种方式,并且也会说明如何把自定义窗口部 ...

  8. C++ GUI Qt4 编程 (第二版)

    [加拿大]JasminBlanchette [英]MarkSummerfield . 电子工业 2008. 前几天的问题多是因为版本不兼容的问题. QT本身Q4 Q5就有版本问题,然后集成到VS08 ...

  9. C++ GUI Qt4编程(13)-6.2preferencedialog

    1. 主要介绍了QStackedLayout.QListWidget.QDialogButtonBox的简单用法.2. QStackedLayout:   要使某个特定的子窗口部件可见,可以用setC ...

随机推荐

  1. Part5核心初始化_lesson2---设置svc模式

    我们的Linux系统以及bootloader是工作在SVC模式!!怎么把处理器设置为SVC模式呢? CPSR寄存器或者SPSR寄存器最低5位可以设置模式,把该5位设置为0b10011, start.s ...

  2. 再谈JQuery插件$.extend(), $.fn和$.fn.extend()

    在我的博客中,曾经写过一篇关于JQuery插件的文章  https://www.cnblogs.com/wphl-27/p/6903170.html 今天看一个项目的代码时,看到使用JQuery插件部 ...

  3. VS2013中全局属性与局部属性的设置

    为了更好的体现程序与库的独立性,vc++2013 中库路径设置不再采用全局设置,就是说在每个工程中都可以有自己独立的库路径设置,当然你如果不设置,那默认就是vs2013自己的库路径.但是如果你需要用到 ...

  4. [docker]Kubernetes的yaml文件

    yaml是一种专门用来写配置的语言,简洁强大 它的规则: 1.大小写敏感 2.使用缩进表示层级关系,但不支持tab缩进,只支持空格 3.缩进的数量不重要但至少一个空格,只要相同层级使用相同数量的空格即 ...

  5. git之对比svn

    关于git的发展和历史介绍网上有很多资料,大家可以自行去了解,这里给大家一个传送门git介绍在这里我就不多说了.我们今天本篇文章的定位就是帮助大家来了解一下关于git和svn之间的区别及git的安装. ...

  6. 编写高质量代码改善C#程序的157个建议——建议21:选择正确的集合

    建议21:选择正确的集合 要选择正确的集合,首先要了解一些数据结构的知识.所谓数据结构,就是相互之间存在一种或多种特定关系的数据元素的集合. 集合的分类参考下图: 由于非泛型集合存在效率低及非类型安全 ...

  7. Python实现wc.exe

    github传送门 项目相关要求 基本功能 -c file.c 返回文件file.c的字符数 (实现) -w file.c 返回文件file.c的词的数目(实现) -l file.c 返回文件file ...

  8. Head First HTML与CSS、XHTML (中文版).(Elisabeth Freeman) PDF扫描版​

    面对那些晦涩的html书你不禁要问:“难道要成为专家之后才能读懂这些?”那么,你应该选择<head first html与css.xhtml(中文版)>真正来学习html.这本书对你来说, ...

  9. 什么是“光照度(Illuminance)”?

    光照度是光度学的概念,了解光照度,要从人眼的特性说起. 人眼的光谱响应 传统的辐射度学的概念(如“功率”,单位为“瓦”)可以客观描述“能量”,但当其用来描述“光照”时却是不合适的,原因在于:人眼对不同 ...

  10. Android中如何下载文件并显示下载进度

    原文地址:http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1125/2057.html 这里主要讨论三种方式:AsyncTask.Serv ...