菜单界面的实现。

看书上第三章,好长,好多代码。我敲了半天,想看看效果,结果却显示不出来。仔细一看,发现spreadsheet的实现在第四章。郁闷....

又到官网上下代码,结果居然不能运行。难道是因为我的版本太高了?

只好自己改,把没实现的部分都先忽略掉,即忽略掉具体的功能,只是显示菜单。折腾了半天,搞定了。

总结一下:

创建菜单:

需要再主窗口类中声明

1.QMenu * 代表一个菜单

2.QAction * 代表菜单中的一个动作 一般一个菜单里会有很多歌动作

3.每个动作对应的槽函数

然后在实现中:

QMenu 的创建,拿fileMenu举例:

主要是1.在窗口中生成菜单 2.添加动作

//文件菜单
fileMenu = menuBar()->addMenu(tr("&File")); //设置显示的名称
fileMenu->addAction(newAction); //添加动作
fileMenu->addAction(openAction);
fileMenu->addAction(saveAction);
fileMenu->addAction(saveAsAction);
separatorAction = fileMenu->addSeparator(); //插入间隔器
for(int i = ; i < MaxRecentFiles; i++)
{
fileMenu->addAction(recentFileActions[i]);
}
fileMenu->addSeparator();
fileMenu->addAction(exitAction);

QAction 的创建,主要包括:

1.新建动作

2.设置动作的图标

3.设置快捷键

4.设置提示 (好奇怪,我设置的提示都显示不出来)

5.添加信号和槽的连接

拿newAction举例:

    newAction = new QAction(tr("&New"),this);
newAction->setIcon(QIcon(":file/images/icon.jpg"));
newAction->setShortcut(QKeySequence::New); //有标准化序列的 没有就用 tr("Ctrl + Q") 之类的
newAction->setStatusTip(tr("Create a new spreadsheet file"));
connect(newAction, SIGNAL(triggered()),this, SLOT(newFile()));

功能函数:

由于只看看样子,都设成空函数就好了。

创建工具栏:

需要声明 QToolBar *

需要的动作和上面是一样的

创建:

    fileToolBar = addToolBar(tr("&File"));
fileToolBar->addAction(newAction);
fileToolBar->addAction(openAction);
fileToolBar->addAction(saveAction);

创建状态栏:

用 QLabel

代码如下:

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "finddialog.h"
#include "gotocelldialog.h"
#include "sortdialog.h" MainWindow::MainWindow()
{
//spreadsheet = new Spreadsheet;
//setCentralWidget(spreadsheet); //设置sheet为主窗口的中央窗口 //创建主窗口的其他部分 createActions();
createMenus();
//createContextMenu();
createToolBars();
//createStatusBar();
//读取应用程序存储的设置
//readSettings();
//find 初始化为空指针
//findDialog = 0; setWindowIcon(QIcon(":file/images/icon.jpg"));
//setCurrentFile("");
} void MainWindow::createActions()
{
/**************File 菜单动作创建***************************/
//新建
newAction = new QAction(tr("&New"),this);
newAction->setIcon(QIcon(":file/images/icon.jpg"));
newAction->setShortcut(QKeySequence::New);
newAction->setStatusTip(tr("Create a new spreadsheet file"));
connect(newAction, SIGNAL(triggered()),this, SLOT(newFile()));
//打开
openAction = new QAction(tr("&Open"),this);
openAction->setIcon(QIcon(":file/images/icon.jpg"));
openAction->setShortcut(QKeySequence::Open);
openAction->setStatusTip(tr("Open a file"));
connect(openAction, SIGNAL(triggered()),this, SLOT(open()));
//保存
saveAction = new QAction(tr("&Save"),this);
saveAction->setIcon(QIcon(":file/images/icon.jpg"));
saveAction->setShortcut(QKeySequence::Save);
saveAction->setStatusTip(tr("Save the file"));
connect(saveAction, SIGNAL(triggered()),this, SLOT(save()));
//另存为
saveAsAction = new QAction(tr("&Save As"),this);
saveAsAction->setIcon(QIcon(":file/images/icon.jpg"));
saveAsAction->setShortcut(QKeySequence::SaveAs);
saveAsAction->setStatusTip(tr("Save the file As"));
connect(saveAsAction, SIGNAL(triggered()),this, SLOT(saveAs()));
//最近打开的文件 for(int i = ; i < MaxRecentFiles; i++)
{
recentFileActions[i] = new QAction(this);
recentFileActions[i]->setVisible(false);
connect(recentFileActions[i], SIGNAL(triggered()),this,SLOT(openRecentFile()));
} //退出
exitAction = new QAction(tr("E&xit"),this);
exitAction->setShortcut(tr("Ctrl+Q"));//没有终止程序的标准化序列键 需要明确指定
exitAction->setStatusTip(tr("Exit the application"));
connect(exitAction, SIGNAL(triggered()),this,SLOT(close())); /********************Edit 菜单动作创建***************************/
cutAction = new QAction(tr("Cu&t"), this);
cutAction->setIcon(QIcon(":file/images/icon.jpg"));
cutAction->setShortcut(QKeySequence::Cut);
cutAction->setStatusTip(tr("Cut the current selection's contents "
"to the clipboard"));
// connect(cutAction, SIGNAL(triggered()), spreadsheet, SLOT(cut())); copyAction = new QAction(tr("&Copy"), this);
copyAction->setIcon(QIcon(":file/images/icon.jpg"));
copyAction->setShortcut(QKeySequence::Copy);
copyAction->setStatusTip(tr("Copy the current selection's contents "
"to the clipboard"));
// connect(copyAction, SIGNAL(triggered()), spreadsheet, SLOT(copy())); pasteAction = new QAction(tr("&Paste"), this);
pasteAction->setIcon(QIcon(":file/images/icon.jpg"));
pasteAction->setShortcut(QKeySequence::Paste);
pasteAction->setStatusTip(tr("Paste the clipboard's contents into "
"the current selection"));
// connect(pasteAction, SIGNAL(triggered()),
// spreadsheet, SLOT(paste())); deleteAction = new QAction(tr("&Delete"), this);
deleteAction->setShortcut(QKeySequence::Delete);
deleteAction->setStatusTip(tr("Delete the current selection's "
"contents"));
//connect(deleteAction, SIGNAL(triggered()),
// spreadsheet, SLOT(del())); //全选
selectAllAction = new QAction(tr("&All"),this);
selectAllAction->setShortcut(QKeySequence::SelectAll);
selectAllAction->setStatusTip(tr("Select all the cells in the 'spreadsheet'"));
connect(selectAllAction, SIGNAL(triggered()),this, SLOT(selectAll())); //Qt已经实现了全选 selectRowAction = new QAction(tr("&Row"), this);
selectRowAction->setStatusTip(tr("Select all the cells in the "
"current row"));
//connect(selectRowAction, SIGNAL(triggered()),
// spreadsheet, SLOT(selectCurrentRow())); selectColumnAction = new QAction(tr("&Column"), this);
selectColumnAction->setStatusTip(tr("Select all the cells in the "
"current column"));
//connect(selectColumnAction, SIGNAL(triggered()),
// spreadsheet, SLOT(selectCurrentColumn())); findAction = new QAction(tr("&Find..."), this);
findAction->setIcon(QIcon(":file/images/icon.jpg"));
findAction->setShortcut(QKeySequence::Find);
findAction->setStatusTip(tr("Find a matching cell"));
connect(findAction, SIGNAL(triggered()), this, SLOT(find())); goToCellAction = new QAction(tr("&Go to Cell..."), this);
goToCellAction->setIcon(QIcon(":file/images/icon.jpg"));
goToCellAction->setShortcut(tr("Ctrl+G"));
goToCellAction->setStatusTip(tr("Go to the specified cell"));
connect(goToCellAction, SIGNAL(triggered()),
this, SLOT(goToCell())); /********************Tools 菜单动作创建***************************/
recalculateAction = new QAction(tr("&Recalculate"), this);
recalculateAction->setShortcut(tr("F9"));
recalculateAction->setStatusTip(tr("Recalculate all the "
"spreadsheet's formulas"));
// connect(recalculateAction, SIGNAL(triggered()),
// spreadsheet, SLOT(recalculate())); sortAction = new QAction(tr("&Sort..."), this);
sortAction->setStatusTip(tr("Sort the selected cells or all the "
"cells"));
// connect(sortAction, SIGNAL(triggered()), this, SLOT(sort())); /********************options 菜单动作创建***************************/
//显示网格 用切换按钮 toggle
showGridAction = new QAction(tr("&Show Grid"),this);
showGridAction->setCheckable(true);
//showGridAction->setChecked(spreadsheet->showGrid());
showGridAction->setStatusTip(tr("Show or hide the spreadsheet's grid"));
//connect(showGridAction, SIGNAL(toggled(bool)),this, SLOT(setShowGrid(bool))); autoRecalcAction = new QAction(tr("&Auto-Recalculate"), this);
autoRecalcAction->setCheckable(true);
//autoRecalcAction->setChecked(spreadsheet->autoRecalculate());
autoRecalcAction->setStatusTip(tr("Switch auto-recalculation on or "
"off"));
//connect(autoRecalcAction, SIGNAL(toggled(bool)),
// spreadsheet, SLOT(setAutoRecalculate(bool))); aboutAction = new QAction(tr("&About"), this);
aboutAction->setStatusTip(tr("Show the application's About box"));
//connect(aboutAction, SIGNAL(triggered()), this, SLOT(about())); aboutQtAction = new QAction(tr("About &Qt"), this);
aboutQtAction->setStatusTip(tr("Show the Qt library's About box"));
//connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
} void MainWindow::createMenus()
{
//文件菜单
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAction);
fileMenu->addAction(openAction);
fileMenu->addAction(saveAction);
fileMenu->addAction(saveAsAction);
separatorAction = fileMenu->addSeparator(); //插入间隔器
for(int i = ; i < MaxRecentFiles; i++)
{
fileMenu->addAction(recentFileActions[i]);
}
fileMenu->addSeparator();
fileMenu->addAction(exitAction); //编辑菜单
editMenu = menuBar()->addMenu(tr("&Edit"));
editMenu->addAction(cutAction);
editMenu->addAction(copyAction);
editMenu->addAction(pasteAction);
editMenu->addAction(deleteAction);
selectSubMenu = editMenu->addMenu(tr("&Select")); //edit菜单的子菜单
selectSubMenu->addAction(selectRowAction);
selectSubMenu->addAction(selectColumnAction);
selectSubMenu->addAction(selectAllAction);
editMenu->addSeparator();
editMenu->addAction(findAction);
editMenu->addAction(goToCellAction); //工具菜单
toolsMenu = menuBar()->addMenu(tr("&Tools"));
toolsMenu->addAction(recalculateAction);
toolsMenu->addAction(sortAction); //options 菜单
optionsMenu = menuBar()->addMenu(tr("&Options"));
optionsMenu->addAction(showGridAction);
optionsMenu->addAction(autoRecalcAction); menuBar()->addSeparator(); //help 菜单
helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutAction);
helpMenu->addAction(aboutQtAction);
} void MainWindow::createToolBars()
{
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()
{
locationLabel = new QLabel(" W999 ");
locationLabel->setAlignment(Qt::AlignHCenter);
locationLabel->setMinimumSize(locationLabel->sizeHint()); formulaLabel = new QLabel;
formulaLabel->setIndent(); statusBar()->addWidget(locationLabel);
statusBar()->addWidget(formulaLabel, ); //connect(spreadsheet, SIGNAL(currentCellChanged(int, int, int, int)),
// this, SLOT(updateStatusBar()));
//connect(spreadsheet, SIGNAL(modified()),
// this, SLOT(spreadsheetModified())); //updateStatusBar();
} bool MainWindow::okToContinue()
{
if (isWindowModified()) {
int r = QMessageBox::warning(this, tr("Spreadsheet"),
tr("The document has been modified.\n"
"Do you want to save your changes?"),
QMessageBox::Yes | QMessageBox::No
| QMessageBox::Cancel);
if (r == QMessageBox::Yes) {
return save();
} else if (r == QMessageBox::Cancel) {
return false;
}
}
return true;
} void MainWindow::newFile()
{
if (okToContinue()) {
}
} void MainWindow::open()
{
if (okToContinue()) {
}
} bool MainWindow::save()
{
if (curFile.isEmpty()) {
return saveAs();
} else {
return saveFile(curFile);
}
} bool MainWindow::saveAs()
{
return false;
} void MainWindow::openRecentFile()
{
} bool MainWindow::saveFile(const QString &fileName)
{
return true;
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow>
#include <QMessageBox>
class QAction;
class QLabel;
class FindDialog;
class Spreadsheet; class MainWindow : public QMainWindow
{
Q_OBJECT public:
MainWindow(); //protected:
// void closeEvent(QCloseEvent * event); //QWidget类中的虚函数 重新实现 可以询问是否保存
private slots:
void newFile();
void open();
bool save();
bool saveAs();
// void find();
// void goToCell();
// void sort();
// void about();
void openRecentFile();
// void updateStatusBar();
// void SpreadsheetModified(); private:
void createActions();
void createMenus();
// void createContextMenu();
void createToolBars();
void createStatusBar();
// void readSettings();
// void writeSettings();
bool okToContinue();
// bool loadFile(const QString &fileName);
bool saveFile(const QString &fileName);
// void setCurrentFile(const QString &fileName);
// void updateRecentFileActions();
// QString strippedName(const QString &fullFileName); // Spreadsheet *spreadsheet;
// FindDialog *findDialog; // QStringList recentFiles;
QString curFile; /**********菜单栏*************/
QMenu *fileMenu;
QMenu *editMenu;
QMenu *selectSubMenu;
QMenu *toolsMenu;
QMenu *optionsMenu;
QMenu *helpMenu;
enum{MaxRecentFiles = };
QAction *recentFileActions[MaxRecentFiles];
QAction *separatorAction; /*********工具栏**********/
QToolBar * fileToolBar;
QToolBar * editToolBar; /***********状态栏*********/
QLabel * locationLabel;
QLabel * formulaLabel; //file菜单选项
QAction * newAction;
QAction * openAction;
QAction * saveAction;
QAction * saveAsAction;
QAction * exitAction;
//edit菜单选项
QAction * cutAction;
QAction * copyAction;
QAction * pasteAction;
QAction * deleteAction;
QAction * selectColumnAction;
QAction * selectRowAction;
QAction * selectAllAction;
QAction * findAction;
QAction * goToCellAction;
//tools菜单选项
QAction * recalculateAction;
QAction * sortAction;
//options菜单选项
QAction * showGridAction;
QAction * autoRecalcAction;
//help菜单选项
QAction * aboutAction;
QAction * aboutQtAction; }; #endif // MAINWINDOW_H

main.cpp

#include "mainwindow.h"
#include <QApplication> int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show(); return a.exec();
}

效果如下:我偷懒,所有的图标都用同一张图。图是我用processing画的,是匡的第一个拼音K,哈哈。

【QT】C++ GUI Qt4 学习笔记3的更多相关文章

  1. C++ GUI Qt4学习笔记01

    C++ GUI Qt4学习笔记01   qtc++signalmakefile文档平台 这一章介绍了如何把基本的C++只是与Qt所提供的功能组合起来创建一些简单的图形用户界面应用程序. 引入两个重要概 ...

  2. C++ GUI Qt4学习笔记03

    C++ GUI Qt4学习笔记03   qtc++spreadsheet文档工具resources 本章介绍创建Spreadsheet应用程序的主窗口 1.子类化QMainWindow 通过子类化QM ...

  3. C++ GUI Qt4学习笔记08

    C++ GUI Qt4学习笔记08   qtc++signal图形引擎文档 本章介绍Qt的二维图形引擎,Qt的二维图形引擎是基于QPainter类的.<span style="colo ...

  4. C++ GUI Qt4学习笔记09

    C++ GUI Qt4学习笔记09   qtc++ 本章介绍Qt中的拖放 拖放是一个应用程序内或者多个应用程序之间传递信息的一种直观的现代操作方式.除了剪贴板提供支持外,通常它还提供数据移动和复制的功 ...

  5. C++ GUI Qt4学习笔记05

    C++ GUI Qt4学习笔记05   qtc++正则表达式 QIntValidator           --  只让用户输入整数 QDoubleValidator     --  只让用户输入浮 ...

  6. C++ GUI Qt4学习笔记07

    C++ GUI Qt4   qtc++scrollobject编程 事件(event)是由串口系统或者Qt自身产生的,用以响应所发生的各类事情.当用户按下或者松开键盘或者鼠标上的按键时,就可以产生一个 ...

  7. 【QT】C++ GUI Qt4 学习笔记1

    Find对话框实现 平台 Qt5.3.2 MinGW4.8.2 注意创建时用QDialog finddialog.h #ifndef FINDDIALOG_H #define FINDDIALOG_H ...

  8. 【QT】C++ GUI Qt4 学习笔记2

    Go To Cell 利用QT Desinger做好界面后加入的代码有 gotocelldialog.h #ifndef GOTOCELLDIALOG_H #define GOTOCELLDIALOG ...

  9. 【QT】C++ GUI Qt4 学习笔记4

    感觉这本书的顺序设计的太不合理了,出现的最多的一句话就是后面会讲.按照使用的顺序讲不行吗?搞得代码都运行不了. 我决定先直接跳到73页,子类化QTableWidgetItem这一节.因为前面功能的实现 ...

随机推荐

  1. [译]Mongoose指南 - Plugin

    Schema支持插件, 这样你就可以扩展一些额功能了 下面的例子是当document save的时候自定更新最后修改日期的出插件 // lastMod.js module.exports = expo ...

  2. 在PHP中$_SESSION的使用方法

    使用PHP应用session时,将session中的数据存储在服务器上,然后通过客户端传来的sessionID识别客户端的信息,并提取信息. php中的session的常用操作:session的写入. ...

  3. 【PHP面向对象(OOP)编程入门教程】18.__call()处理调用错误

    在程序开发中,如果在使用对象调用对象内部方法时候,调用的这个方法不存在那么程序就会出错,然后程序退出不能继续执行.那么可不可以在程序调用对象内部 不存在的方法时,提示我们调用的方法及使用的参数不存在, ...

  4. centos 6.5 zabbix3.0.4 监控apache

    开启apache的server-status httpd.conf 末尾添加 [root@test3 /]# vim /usr/local/httpd-/conf/httpd.conf Extende ...

  5. utils部分--一些通用的工具类封装

    1.简介 utils部分是对一些常用的工具类进行简单的封装,使用起来比较方便.这里列举常用的一些. 2.ContextUtils使用 主要封装了网络判断.一些方法解释如下: ? 1 2 3 4 5 6 ...

  6. BPMN流程图的绘制的注意要点

    1.分支网关的表达式,是在选择的线上设置. 2.在分支网关上,可以设置一个默认线的id. 3.并行网关,必须有开始,有结束.

  7. iOS开发——UI进阶篇(十六)Quartz2D实战小例子

    一.画线 只有在drawRect中才能获取到跟view相关联的上下文 - (void)drawRect:(CGRect)rect {} 一条线 // 1.获取跟当前View相关联的layer上下文(画 ...

  8. SQL手册

    来自 W3School 的 SQL 快速参考.可以打印它,以备日常使用. SQL 语句 语句 语法 AND / OR SELECT column_name(s)FROM table_nameWHERE ...

  9. 如何用Wireshark捕获USB数据?

    现在越来越多的电子设备采用USB接口进行通讯,通讯标准也在逐步提高.那么,我们就会好奇这些设备是如何工作的?而无论你是一个硬件黑客,业余爱好者或者只是对它有一点兴趣的,USB对我们都是具有挑战性的. ...

  10. 用 ROS 做内网DNS服务器

    转载:http://iliuyong.iteye.com/blog/1035692 用 ROS 做内网DNS服务器方法:1.ROS 设置IP ->DNS 选择"static" ...