1. C++ GUI Qt4编程第三章,增加工具栏、状态栏和快捷键。

2. mainwindow.h

 /**/
#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow> class QMenu;
class QAction;
class QToolBar;
class QLabel; 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; /*工具栏*/
QToolBar *fileToolBar;
QToolBar *editToolBar; /*状态栏标签*/
QLabel *locationLabel;
QLabel *formulaLabel; void createMenus();
void createActions();
void createFileActions();
void createEditActions();
void createToolsActions();
void createOptionsActions();
void createHelpAction();
void createToolBar();
void createStatusBar();
}; #endif /*MAINWINDOW_H*/

3. mainwindow.cpp

 /**/
#include <QMenu>
#include <QMenuBar>
#include <QAction>
#include <QToolBar>
#include <QLabel>
#include <QStatusBar>
#include "mainwindow.h" MainWindow::MainWindow()
{
createActions();
createMenus();
createToolBar();
createStatusBar();
} 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*/
newAction = new QAction(tr("&New"), this); /*加速键 Alt+N*/
newAction->setIcon(QIcon(":/images/filenew.png")); /*图标*/
newAction->setShortcut(QKeySequence::New); /*快捷键 Ctrl+N*/
// newAction->setShortcut(tr("Ctrl+N")); /*快捷键的另一种方法*/
/*状态栏显示内容*/
newAction->setStatusTip(tr("Create a new spreadsheet file")); /*openAction*/
openAction = new QAction(tr("&Open"), this);
openAction->setIcon(QIcon(":/images/fileopen.png"));
openAction->setShortcut(QKeySequence::Open);
openAction->setStatusTip(tr("Opne an existing spreadsheet file")); /*saveAction*/
saveAction = new QAction(tr("&Save"), this);
saveAction->setIcon(QIcon(":/images/filesave.png"));
saveAction->setShortcut(QKeySequence::Save);
saveAction->setStatusTip(tr("Save the spreadsheet to disk")); /*saveAsAction*/
saveAsAction = new QAction(tr("Save &As..."), this);
saveAsAction->setIcon(QIcon(":/images/filesaveas.png"));
saveAsAction->setShortcut(QKeySequence::SaveAs);
saveAsAction->setStatusTip(tr("Save the spreadsheet under a new name")); /*exitAction */
exitAction = new QAction(tr("E&xit"), this);
exitAction->setShortcut(tr("Ctrl+Q"));
exitAction->setStatusTip(tr("Exit the application"));
} /*edit动作*/
void MainWindow::createEditActions()
{
/*cutAction*/
cutAction = new QAction(tr("Cu&t"), this);
cutAction->setIcon(QIcon(":/images/editcut.png"));
cutAction->setShortcut(QKeySequence::Cut);
cutAction->setStatusTip(tr("Cut the Current selection's "
"contents to the clipboard")); /*copyAction*/
copyAction = new QAction(tr("&Copy"), this);
copyAction->setIcon(QIcon(":/images/editcopy.png"));
copyAction->setShortcut(QKeySequence::Copy);
copyAction->setStatusTip(tr("Copy the current selection's "
"contents to the clipboard")); /*pasteAction*/
pasteAction = new QAction(tr("&Paste"), this);
pasteAction->setIcon(QIcon(":/images/editpaste.png"));
pasteAction->setShortcut(QKeySequence::Paste);
pasteAction->setStatusTip(tr("Paste the clipboard's "
"contents into the current selection")); /*deleteAction*/
deleteAction = new QAction(tr("&Delete"), this);
deleteAction->setIcon(QIcon(":/images/editdelete.png"));
deleteAction->setShortcut(QKeySequence::Delete);
deleteAction->setStatusTip(tr("Delete the current selection's "
"contents")); selectRowAction = new QAction(tr("&Row"), this);
selectRowAction->setStatusTip(tr("Select all the cells in "
"the current row"));
selectColumnAction = new QAction(tr("&Column"), this);
selectColumnAction->setStatusTip(tr("Select all the cells in "
"the current column"));
selectAllAction = new QAction(tr("&All"), this);
selectAllAction->setShortcut(QKeySequence::SelectAll);
selectAllAction->setStatusTip(tr("Select all the cells in "
"the spreadsheet")); /*findAction*/
findAction = new QAction(tr("&Find..."), this);
findAction->setIcon(QIcon(":/images/editfind.png"));
findAction->setShortcut(QKeySequence::Find);
findAction->setStatusTip(tr("Find a matching cell")); /*goToCellAction*/
goToCellAction = new QAction(tr("&Go to Cell..."), this);
goToCellAction->setIcon(QIcon(":/images/editgotocell"));
goToCellAction->setShortcut(tr("Ctrl+G"));
} /*tools动作*/
void MainWindow::createToolsActions()
{
recalculateAction = new QAction(tr("&Recalculate"), this);
recalculateAction->setShortcut(tr("F9"));
recalculateAction->setStatusTip(tr("Recalculate all the "
"spreadsheet's formulas")); sortAction = new QAction(tr("&Sort..."), this);
sortAction->setStatusTip(tr("Sort the selected cells or all "
"the cells"));
} /*options动作*/
void MainWindow::createOptionsActions()
{
showGridAction = new QAction(tr("&Show Grid"), this);
showGridAction->setCheckable(true); /*使动作可被选*/
showGridAction->setStatusTip(tr("Show or hide the "
"spreadsheet's grid")); autoRecalculateAction = new QAction(tr("Auto-Recalculate"), this);
autoRecalculateAction->setCheckable(true); /*使动作可被选*/
autoRecalculateAction->setStatusTip(tr("Switch auto-"
"recalculate on or off"));
} /*help动作*/
void MainWindow::createHelpAction()
{
aboutAction = new QAction(tr("&About"), this);
aboutAction->setStatusTip(tr("Show the application's "
"About box")); aboutQtAction = new QAction(tr("About &Qt"), this);
aboutQtAction->setStatusTip(tr("Show the Qt library's "
"About box"));
} /*工具栏*/
void MainWindow::createToolBar()
{
fileToolBar = addToolBar(tr("&File"));
fileToolBar->addAction(newAction);
fileToolBar->addAction(openAction);
fileToolBar->addAction(saveAction); editToolBar = addToolBar(tr("&Edit"));
editToolBar->addAction(cutAction);
editToolBar->addAction(copyAction);
editToolBar->addAction(pasteAction);
editToolBar->addSeparator();
editToolBar->addAction(findAction);
editToolBar->addAction(goToCellAction);
} /*状态栏*/
void MainWindow::createStatusBar()
{
/* W999 作用:1.显示的内容。2.决定locationLabel的尺寸大小*/
locationLabel = new QLabel(" W999 ");
/*对齐方式:居中对齐*/
locationLabel->setAlignment(Qt::AlignHCenter);
/*最小大小为窗口部件的理想大小*/
locationLabel->setMinimumSize(locationLabel->sizeHint()); formulaLabel = new QLabel;
/*缩进,文本与左侧边的偏移量*/
formulaLabel->setIndent(); /*单元格定位指示器,伸展因子默认为0*/
statusBar()->addWidget(locationLabel);
/*单元格公式指示器,伸展因子为1*/
statusBar()->addWidget(formulaLabel, );
}

5. main.cpp和spreadsheet.qrc不变。

C++ GUI Qt4编程(09)-3.3spreadsheet-toolbar的更多相关文章

  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编程(08)-3.2spreadsheet-resource

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

  3. C++ GUI Qt4编程(07)-3.1menu

    1. C++ GUI Qt4编程第三章,添加menu菜单. 2. mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include < ...

  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编程(05)-2.2GoToCell

    1. 使用Qt设计师创建GoToCell对话框. 2. gotocelldialog.cpp #include <QRegExp> #include "gotocelldialo ...

随机推荐

  1. CF547D Mike and Fish

    欧拉回路,巧妙的解法. 发现每一个点$(x, y)$实际上是把横坐标和$x$和纵坐标$y$连一条线,然后代进去跑欧拉回路,这样里一条边对应了一个点,我们只要按照欧拉回路间隔染色即可. 注意到原图可能并 ...

  2. Flask框架 之 wtforms

    简介 WTForms是一个支持多个web框架的form组件,主要用于对用户请求数据进行验证. 作用 生成HTML标签 form表单验证 使用 - 用户登录示例- 用户注册示例- 数据库获取数据实时更新 ...

  3. YDNJS(上卷):this 的绑定对象

    函数中的 this 是在调用时被绑定的,this 指向谁完全取决于函数的调用位置. 确定 this 的绑定对象的方式有 4 种. 默认绑定 默认绑定就是将函数中的 this 绑定给了全局对象 wind ...

  4. 如何处理与开发有争议的Bug?

     工作中,测试人员有时会遇到类似的问题:提交了一份软件缺陷报告,可由于某种原因,无论是开发人员还是开发经理就是不愿修改程序.应如何处理这类问题呢?我认为,当对报告出现分歧意见后,测试工程师应首先做如下 ...

  5. App测试从入门到精通之安装、卸载和运行测试

    关于手机App测试需要说的点有很多.目前市场上主要的APP测试主要是针对的是安卓.和苹果两大主流操作系统.主要考虑的就是功能性.兼容性.稳定性.性能测试等.我们看下App的安装和卸载有哪些常用的场景: ...

  6. linux学习之路(4)

    用户身份与文件权限 通过uid来区分:  管理员 UID 为 0:系统的管理员用户. 系统用户 UID 为 1-999: Linux 系统为了避免因某个服务程序出现漏洞而被黑客提 权至整台服务器,默认 ...

  7. 百度地图离线API及地图数据下载工具

    全面介绍,请看下列介绍地址,改写目前最新版本的百度V2.0地图,已全面实现离线操作,能到达在线功能的95%以上 http://api.jjszd.com:8081/apituiguang/gistg. ...

  8. Xcode9新功能

    1.折叠代码 局部折叠(折叠一个函数):Command+Option+Left/Right 全局折叠(折叠当前文件下的全部函数): Shift+Command+Option+Left/Right 折叠 ...

  9. ubuntu命令行安装tomcat8

    环境: 虚拟机VM14 Ubuntu16.04 java 1.8 步骤: 先更新 sudo apt-get update 然后安装: sudo apt-get install tomcat8 等一会 ...

  10. PHP项目目录结构

    PHP项目目录结构 原创 2017年11月23日 16:02:18 标签: php / 结构 1226 一个完整的项目需要有三大部分构成,项目框架,业务实现,公共支持.为了便于开发维护,通常使三部分分 ...