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 ...
随机推荐
- laravel与front-end
准备工作 在此之前要安装node . npm .这里安装node . npm 就不介绍了,百度一大把. 安装所有的npm依赖包 //进入项目的根目录 npm install 安装完后会出现一个nod ...
- 通过网页发布ios应用。
原文地址:http://www.zhihu.com/question/24304345 两种方法: 1. 测试版本 支持任何类型的开发者帐号,需要在developer后台设置授权deviceID,可以 ...
- 我用Django搭网站(2)-QQ登录
接入QQ登录前,网站需首先进行申请,获得对应的appid与appkey,以保证后续流程中可正确对网站与用户进行验证与授权. 第一步:准备阶段 打开QQ互联,并登录你的QQ账号.再点击导航上的" ...
- php 可变数量的参数列表
可变数量的参数列表 PHP 在用户自定义函数中支持可变数量的参数列表.在 PHP 5.6 及以上的版本中,由 ... 语法实现:在 PHP 5.5 及更早版本中,使用函数func_num_args() ...
- Invoke()的使用
(最近在看协程) Invoke()方法是一种委托机制 Invoke ( "SendMsg", 3 ), 意思是3秒之后调用 SendMsg() 方法 使用时应该注意以下几点: 1. ...
- C#操作excel打印
using System; using System.Data; using System.IO; using System.Runtime.InteropServices; using System ...
- 将以太坊封装为 ERC20
将以太坊封装为 ERC20 TOKEN 很多 DAPP 都是在处理 ERC20接口的 token, 其实很容易将以太坊封装为 ERC20,这样就可以统一处理, 至少我目前在做的雷电网络就是这么处理的. ...
- [转] Draw Call未被批处理?告诉你在Unity 5.6中如何查找原因 [复制链接]
Unity在5.6之前的版本中并未提供很直接的方式来查找Draw Call未被批处理的原因,但Unity 5.6在Frame Debugger中新增了一项功能,帮助开发者查找相关信息.今天这篇文章就为 ...
- SKU:唯一标识填什么
策略 随意填写 只要别和别人重复就好 ,不过重复你也创建不了. 最好填与APP信息相关的,比如直接填写bundle ID 上去...跟套装ID保持一致. 你新建应用的时候都还没有APP ID 你怎么填 ...
- kali 插耳机没声音
打开终端,然后输入命令:下载pulseaudio音量控制软件: apt install pavucontrol 然后终端输入指令:pavucontrol打开软件,发现在输出设备中有两个输出设备:一个N ...