C++ GUI Qt4编程(10)-3.4spreadsheet
1. C++ GUI Qt4编程第三章,增加spreadsheet。
2. spreadsheet.h
/**/
#ifndef SPREADSHEET_H
#define SPREADSHEET_H #include <QTableWidget> class Spreadsheet : public QTableWidget
{
Q_OBJECT public:
Spreadsheet(QWidget *parent = );
void clear(); private:
enum{RowCount = , ColumnCount = };
}; #endif
3. spreadsheet.cpp
/**/
#include "spreadsheet.h" Spreadsheet::Spreadsheet(QWidget *parent)
: QTableWidget(parent)
{
clear();
} void Spreadsheet::clear()
{
setRowCount(); /*将表格向下调整为0X0,及完全清空表格*/
setColumnCount();
setRowCount(RowCount); /*重新调整表格大小为RowCount X ColumnCount*/
setColumnCount(ColumnCount); for (int i=;i<ColumnCount;++i)
{
QTableWidgetItem *item = new QTableWidgetItem;
/*把QTableWidgetItem水平方向上的标题修改为列名A B ... Z。垂直不用设,默认从1开始。*/
item->setText(QString(QChar('A' + i)));
setHorizontalHeaderItem(i, item);
} setCurrentCell(, ); /*把单元格光标移动到A1处*/
}
4. mainwindow.h
/**/
#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow> class QMenu;
class QAction;
class QToolBar;
class QLabel;
class Spreadsheet; class MainWindow : public QMainWindow
{
Q_OBJECT public:
MainWindow(); private:
Spreadsheet *spreadsheet;
QString currentFileName; /*菜单*/
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();
void setCurrentFile(const QString &fileName);
QString strippedName(const QString &fullFileName);
}; #endif /*MAINWINDOW_H*/
5. mainwindow.cpp
/**/
#include <QMenu>
#include <QMenuBar>
#include <QAction>
#include <QToolBar>
#include <QLabel>
#include <QStatusBar>
#include <QFileInfo>
#include "mainwindow.h"
#include "spreadsheet.h" MainWindow::MainWindow()
{
/*创建Spreadsheet窗口部件*/
spreadsheet = new Spreadsheet(this);
/*设置为中央窗口部件*/
setCentralWidget(spreadsheet); createActions();
createMenus();
createToolBar();
createStatusBar(); setWindowIcon(QIcon(":/images/icon.png"));
setCurrentFile("");
} 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, );
} void MainWindow::setCurrentFile(const QString &fileName)
{
currentFileName = fileName;
/*true:保存过和未保存有区别;false:无区别*/
setWindowModified(true); QString shownName = tr("Untitled"); if (!currentFileName.isEmpty())
{
shownName = strippedName(currentFileName); /*名字去掉路径*/
} /*[*]:当WindowModified属性为true时,保存过的文件才没有星号*/
setWindowTitle(tr("%1[*] - %2").arg(shownName)
.arg("Spreadsheet"));
} /*去掉路径,只保留文件名*/
QString MainWindow::strippedName(const QString &fullFileName)
{
return QFileInfo(fullFileName).fileName();
}
C++ GUI Qt4编程(10)-3.4spreadsheet的更多相关文章
- C++ GUI Qt4编程(09)-3.3spreadsheet-toolbar
1. C++ GUI Qt4编程第三章,增加工具栏.状态栏和快捷键. 2. mainwindow.h /**/ #ifndef MAINWINDOW_H #define MAINWINDOW_H #i ...
- C++ GUI Qt4编程(08)-3.2spreadsheet-resource
1. C++ GUI Qt4编程第三章,图片使用资源机制法. 2. 步骤: 2-1. 在resource文件夹下,新建images文件,存放图片. 2-2. 新建spreadsheet.qrc文件,并 ...
- C++ GUI Qt4编程(07)-3.1menu
1. C++ GUI Qt4编程第三章,添加menu菜单. 2. mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include < ...
- C++ GUI Qt4编程(03)-1.3layout
1. 根据C++ GUI Qt4编程(第二版)整理2. 系统:centos7: Qt版本:5.5.13. 程序:layout.cpp #include <QApplication> #i ...
- C++ GUI Qt4编程(02)-1.2quit
1. 根据C++ GUI Qt4编程(第二版)整理2. 系统:centos7: Qt版本:5.5.13. 程序:quit.cpp #include <QApplication> #inc ...
- C++ GUI Qt4编程(01)-1.1Hello Qt
1. 根据C++ GUI Qt4编程(第二版)整理2. 系统:centos7: Qt版本:5.5.13. 程序:hello.cpp #include <QApplication> #in ...
- C++ GUI Qt4编程-创建自定义窗口部件
C++ GUI Qt4编程-创建自定义窗口部件 Qtqt4 通过Qt窗口部件进行子类化或者直接对QWidget进行子类化,就可以创建自定义窗口部件,下面示范两种方式,并且也会说明如何把自定义窗口部 ...
- C++ GUI Qt4 编程 (第二版)
[加拿大]JasminBlanchette [英]MarkSummerfield . 电子工业 2008. 前几天的问题多是因为版本不兼容的问题. QT本身Q4 Q5就有版本问题,然后集成到VS08 ...
- C++ GUI Qt4编程(13)-6.2preferencedialog
1. 主要介绍了QStackedLayout.QListWidget.QDialogButtonBox的简单用法.2. QStackedLayout: 要使某个特定的子窗口部件可见,可以用setC ...
随机推荐
- Flask框架 之 上下文管理前戏
偏函数 自动传递参数 import functools def index(a1,a2): return a1 + a2 # 原来的调用方式 # ret = index(1,23) # print(r ...
- HttpSession解决表单的重复提交
1). 重复提交的情况: ①. 在表单提交到一个 Servlet, 而 Servlet 又通过请求转发的方式响应一个 JSP(HTML) 页面, 此时地址栏还保留着 Serlvet 的那个路径, 在响 ...
- 构建使用 Azure 网站的云
Apurva JoshiSunitha Muthukrishna 在设计云解决方案时,设计始终要为故障做好准备.这一点很重要,应牢记. 然而,许多应用程序并非按照这种方式构建. 出现这种情况的主要原因 ...
- HDU 3723 Delta Wave (高精度+calelan数)
题意:给定一个图,问你只能向上向下,或者平着走,有多少种方法可以走到最后一个格. 析:首先先考虑,如果没有平的情况就是calelan数了,现在有平的情况,那么就枚举呗,因为数很大,所以要用高精度. 答 ...
- MySQL性能调优与架构设计——第7章 MySQL数据库锁定机制
第7章 MySQL数据库锁定机制 前言: 为了保证数据的一致完整性,任何一个数据库都存在锁定机制.锁定机制的优劣直接应想到一个数据库系统的并发处理能力和性能,所以锁定机制的实现也就成为了各种数据库的核 ...
- ThinkPHP3.2.3完整版中对Auth.class.php的使用
一,先创建数据表 1.think_auth_rule,规则表 id:主键, name:规则唯一标识, title:规则中文名称 status 状态:为1正常,为0禁用, condition:规则表达式 ...
- ajax 判断账户密码 调取数据模糊查询 时钟
一.判断账户密码 <Login.html> <head> <meta http-equiv="Content-Type" content=" ...
- c# 将json转换为DataTable
/// <summary> /// 将json转换为DataTable /// </summary> /// <param name="strJson" ...
- fwrite()
注:fwrite(),fread -可对数据块读写,且数据为二进制,文本下查看为乱码,文件的打开方式为 “b*” 实例: 写入二进制数据 for (int i = 0; i < SN; i++) ...
- [Swift实际操作]九、完整实例-(2)在Xcode 10中创建新项目
本文将在Xcode中创建上一文<在iTunesConnect网站中创建产品>在iTunes Connect创建的产品具有相同的Bundle ID的应用程序. 在项目模板窗口中,选择单视图模 ...