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 ...
随机推荐
- fiddler抓包时显示Tunnel to......443
打开手机浏览器,输入http://192.168.0.65:8888/FiddlerRoot.cer
- Codeforces 429B B. Working out
题目意思: 给n*m的矩阵,每个格子有个数,A从(1,1)出发只能向下或右走,终点为(n,m),B从(n,1)出发只能向上或右走,终点为(1,m).两个人的速度不一样,走到的格子可以获的该格子的数,两 ...
- JSP+JDBC实现在可视化页面中插入数据到SQL数据库
原创 本篇博客创建一个如下图所示的JSP页面,将用户填入的数据插入到对应的数据库中. JSP页面代码: <%@ page language="java" contentTyp ...
- 关于对SwfUpload的改造
Swfupload 在普通上传下,对于IE chrome firefox等有很好的兼容性. 但一旦与其他控件组合,很容易出现无法上传,帮顶事件丢失的情况. 例如Layer与Swfupload,上传一个 ...
- 13本热门书籍免费送!(Python、SpingBoot、Entity Framework、Ionic、MySQL、深度学习、小程序开发等)
七月第一周,网易云社区联合清华大学出版社为大家送出13本数据分析以及移动开发的书籍(Python.SpingBoot.Entity Framework.Ionic.MySQL.深度学习.小程序开发等) ...
- 同一个程序里有多个版本的App
在Xcode中添加多个targets进行版本控制,就是同一个app开发多个版本 以Xcode 9.3 为例 1. 创建 点击左侧工程项目文件,选择TARGETS 下的项目右击选择 Duplicate. ...
- 可变大小、颜色边框、样式的UISwitch
1.CHSwitch.h // // 文 件 名:CHSwitch.h // // 版权所有:Copyright © 2018 lelight. All rights reserved. // 创 建 ...
- requests利用selenium,代理Ip,云打码,验证码抠图操作 爬取搜狗微信公众号内容
爬取思路,爬取搜狗微信公众号内容,爬取第一层url时请求太快出现验证码,我这里用的蘑菇云代理,并在程序中我判断什么情况下是否+代理,做到合理运用代理ip.爬取第二层url时验证码出现次数更严重(和第一 ...
- 洛谷P2770 航空路线问题(费用流)
传送门 完了这题好厉害……字符串什么的好麻烦…… 要求从$1$到$n$的路径,不重复,经过边数最多 每一个点拆成两个,$A_i,B_i$,然后$A_i$到$B_i$连容量为$1$,费用为$1$的边,保 ...
- 如何删除/mnt/cdrom?|如何删除只读文件系统(Read-only files ystem)? failed !bh ? 挂载光盘?挂载usb?
root权限下 : 首先用umount /mnt/文件夹 卸载文件系统,必要时可以用umount -f(可能丢失数据)然后rm -rf /mnt/cdrom mkdir /mnt/cdrom moun ...