码迷,mamicode.com
首页 > 编程语言 > 详细

C++ GUI Qt4编程(10)-3.4spreadsheet

时间:2017-07-01 11:49:57      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:路径   cell   ctr   作用   define   disk   actions   add   tool   

1. C++ GUI Qt4编程第三章,增加spreadsheet。

2. spreadsheet.h

 1 /**/
 2 #ifndef SPREADSHEET_H
 3 #define SPREADSHEET_H
 4 
 5 #include <QTableWidget>
 6 
 7 class Spreadsheet : public QTableWidget
 8 {
 9     Q_OBJECT
10     
11 public:
12     Spreadsheet(QWidget *parent = 0);
13     void clear();
14     
15 private:
16     enum{RowCount = 999, ColumnCount = 26};
17 };
18 
19 #endif

3. spreadsheet.cpp

 1 /**/
 2 #include "spreadsheet.h"
 3 
 4 Spreadsheet::Spreadsheet(QWidget *parent)
 5     : QTableWidget(parent)
 6 {
 7     clear();
 8 }
 9     
10 void Spreadsheet::clear()
11 {
12     setRowCount(0);            /*将表格向下调整为0X0,及完全清空表格*/
13     setColumnCount(0);
14     setRowCount(RowCount);    /*重新调整表格大小为RowCount X ColumnCount*/
15     setColumnCount(ColumnCount);
16     
17     for (int i=0;i<ColumnCount;++i)
18     {
19         QTableWidgetItem *item = new QTableWidgetItem;
20         /*把QTableWidgetItem水平方向上的标题修改为列名A B ... Z。垂直不用设,默认从1开始。*/
21         item->setText(QString(QChar(A + i)));
22         setHorizontalHeaderItem(i, item);
23     }
24     
25     setCurrentCell(0, 0);    /*把单元格光标移动到A1处*/
26 }

4. mainwindow.h

 1 /**/
 2 #ifndef MAINWINDOW_H
 3 #define MAINWINDOW_H
 4 
 5 #include <QMainWindow>
 6 
 7 class QMenu;
 8 class QAction;
 9 class QToolBar;
10 class QLabel;
11 class Spreadsheet;
12 
13 class MainWindow : public QMainWindow
14 {
15     Q_OBJECT
16     
17 public:
18     MainWindow();
19     
20 private:
21     Spreadsheet *spreadsheet;
22     QString currentFileName;
23     
24     /*菜单*/
25     QMenu *fileMenu;
26     QMenu *editMenu;
27     QMenu *toolsMenu;
28     QMenu *optionsMenu;
29     QMenu *helpMenu;
30     
31     /*File动作*/
32     QAction *newAction;
33     QAction *openAction;
34     QAction *saveAction;
35     QAction *saveAsAction;
36     QAction *exitAction;
37     
38     /*Edit动作*/
39     QAction *cutAction;
40     QAction *copyAction;
41     QAction *pasteAction;
42     QAction *deleteAction;
43     QMenu   *selectSubMenu;
44     QAction *selectRowAction;
45     QAction *selectColumnAction;
46     QAction *selectAllAction;
47     QAction *findAction;
48     QAction *goToCellAction;
49     
50     /*Tools动作*/
51     QAction *recalculateAction;
52     QAction *sortAction;
53     
54     /*Options动作*/
55     QAction *showGridAction;
56     QAction *autoRecalculateAction;
57     
58     /*Help动作*/
59     QAction *aboutAction;
60     QAction *aboutQtAction;
61     
62     /*工具栏*/
63     QToolBar *fileToolBar;
64     QToolBar *editToolBar;
65     
66     /*状态栏标签*/
67     QLabel *locationLabel;
68     QLabel *formulaLabel;
69     
70     void createMenus();
71     void createActions();
72     void createFileActions();
73     void createEditActions();
74     void createToolsActions();
75     void createOptionsActions();
76     void createHelpAction();
77     void createToolBar();
78     void createStatusBar();
79     void setCurrentFile(const QString &fileName);
80     QString strippedName(const QString &fullFileName);
81 };
82 
83 #endif    /*MAINWINDOW_H*/

5. mainwindow.cpp

  1 /**/
  2 #include <QMenu>
  3 #include <QMenuBar>
  4 #include <QAction>
  5 #include <QToolBar>
  6 #include <QLabel>
  7 #include <QStatusBar>
  8 #include <QFileInfo>
  9 #include "mainwindow.h"
 10 #include "spreadsheet.h"
 11 
 12 MainWindow::MainWindow()
 13 {
 14     /*创建Spreadsheet窗口部件*/
 15     spreadsheet = new Spreadsheet(this);
 16     /*设置为中央窗口部件*/
 17     setCentralWidget(spreadsheet);
 18     
 19     createActions();
 20     createMenus();
 21     createToolBar();
 22     createStatusBar();
 23     
 24     setWindowIcon(QIcon(":/images/icon.png"));
 25     setCurrentFile("");
 26 }
 27 
 28 void MainWindow::createMenus()
 29 {
 30     /*file menu*/
 31     fileMenu = menuBar()->addMenu(tr("&File"));
 32     fileMenu->addAction(newAction);
 33     fileMenu->addAction(openAction);
 34     fileMenu->addAction(saveAction);
 35     fileMenu->addAction(saveAsAction);
 36     fileMenu->addSeparator();
 37     fileMenu->addAction(exitAction);
 38     
 39     /*edit menu*/
 40     editMenu = menuBar()->addMenu(tr("&Edit"));
 41     editMenu->addAction(cutAction);
 42     editMenu->addAction(copyAction);
 43     editMenu->addAction(pasteAction);
 44     editMenu->addAction(deleteAction);
 45     selectSubMenu = editMenu->addMenu(tr("&Select"));
 46     selectSubMenu->addAction(selectRowAction);
 47     selectSubMenu->addAction(selectColumnAction);
 48     selectSubMenu->addAction(selectAllAction);
 49     editMenu->addSeparator();
 50     editMenu->addAction(findAction);
 51     editMenu->addAction(goToCellAction);
 52     
 53     /*tools menu*/
 54     toolsMenu = menuBar()->addMenu(tr("&Tools"));
 55     toolsMenu->addAction(recalculateAction);
 56     toolsMenu->addAction(sortAction);
 57     
 58     /*option menu*/
 59     optionsMenu = menuBar()->addMenu(tr("&Option"));
 60     optionsMenu->addAction(showGridAction);
 61     optionsMenu->addAction(autoRecalculateAction);
 62     
 63     /*间隔器*/
 64     menuBar()->addSeparator();
 65     
 66     /*help menu*/
 67     helpMenu = menuBar()->addMenu(tr("&Help"));
 68     helpMenu->addAction(aboutAction);
 69     helpMenu->addAction(aboutQtAction);
 70 }
 71 
 72 void MainWindow::createActions()
 73 {
 74     createFileActions();
 75     createEditActions();
 76     createToolsActions();
 77     createOptionsActions();
 78     createHelpAction();
 79 }
 80 
 81 /*
 82  * file动作
 83  * 加速键与快捷键的区别
 84  */
 85 void MainWindow::createFileActions()
 86 {
 87     /*newAction*/
 88     newAction = new QAction(tr("&New"), this);    /*加速键 Alt+N*/
 89     newAction->setIcon(QIcon(":/images/filenew.png"));    /*图标*/
 90     newAction->setShortcut(QKeySequence::New);    /*快捷键 Ctrl+N*/
 91 //    newAction->setShortcut(tr("Ctrl+N"));        /*快捷键的另一种方法*/
 92     /*状态栏显示内容*/
 93     newAction->setStatusTip(tr("Create a new spreadsheet file"));
 94     
 95     /*openAction*/
 96     openAction = new QAction(tr("&Open"), this);
 97     openAction->setIcon(QIcon(":/images/fileopen.png"));
 98     openAction->setShortcut(QKeySequence::Open);
 99     openAction->setStatusTip(tr("Opne an existing spreadsheet file"));
100     
101     /*saveAction*/
102     saveAction = new QAction(tr("&Save"), this);
103     saveAction->setIcon(QIcon(":/images/filesave.png"));
104     saveAction->setShortcut(QKeySequence::Save);
105     saveAction->setStatusTip(tr("Save the spreadsheet to disk"));
106     
107     /*saveAsAction*/
108     saveAsAction = new QAction(tr("Save &As..."), this);
109     saveAsAction->setIcon(QIcon(":/images/filesaveas.png"));
110     saveAsAction->setShortcut(QKeySequence::SaveAs);
111     saveAsAction->setStatusTip(tr("Save the spreadsheet under a new name"));
112     
113     /*exitAction */
114     exitAction = new QAction(tr("E&xit"), this);
115     exitAction->setShortcut(tr("Ctrl+Q"));
116     exitAction->setStatusTip(tr("Exit the application"));
117 }
118 
119 /*edit动作*/
120 void MainWindow::createEditActions()
121 {
122     /*cutAction*/
123     cutAction = new QAction(tr("Cu&t"), this);
124     cutAction->setIcon(QIcon(":/images/editcut.png"));
125     cutAction->setShortcut(QKeySequence::Cut);
126     cutAction->setStatusTip(tr("Cut the Current selection‘s "
127         "contents to the clipboard"));
128     
129     /*copyAction*/
130     copyAction = new QAction(tr("&Copy"), this);
131     copyAction->setIcon(QIcon(":/images/editcopy.png"));
132     copyAction->setShortcut(QKeySequence::Copy);
133     copyAction->setStatusTip(tr("Copy the current selection‘s "
134         "contents to the clipboard"));
135     
136     /*pasteAction*/
137     pasteAction = new QAction(tr("&Paste"), this);
138     pasteAction->setIcon(QIcon(":/images/editpaste.png"));
139     pasteAction->setShortcut(QKeySequence::Paste);
140     pasteAction->setStatusTip(tr("Paste the clipboard‘s "
141         "contents into the current selection"));
142     
143     /*deleteAction*/
144     deleteAction = new QAction(tr("&Delete"), this);
145     deleteAction->setIcon(QIcon(":/images/editdelete.png"));
146     deleteAction->setShortcut(QKeySequence::Delete);
147     deleteAction->setStatusTip(tr("Delete the current selection‘s "     "contents"));
148     
149     selectRowAction = new QAction(tr("&Row"), this);
150     selectRowAction->setStatusTip(tr("Select all the cells in "
151         "the current row"));
152     selectColumnAction = new QAction(tr("&Column"), this);
153     selectColumnAction->setStatusTip(tr("Select all the cells in "
154         "the current column"));
155     selectAllAction = new QAction(tr("&All"), this);
156     selectAllAction->setShortcut(QKeySequence::SelectAll);
157     selectAllAction->setStatusTip(tr("Select all the cells in "
158         "the spreadsheet"));
159     
160     /*findAction*/
161     findAction = new QAction(tr("&Find..."), this);
162     findAction->setIcon(QIcon(":/images/editfind.png"));
163     findAction->setShortcut(QKeySequence::Find);
164     findAction->setStatusTip(tr("Find a matching cell"));
165     
166     /*goToCellAction*/
167     goToCellAction = new QAction(tr("&Go to Cell..."), this);
168     goToCellAction->setIcon(QIcon(":/images/editgotocell"));
169     goToCellAction->setShortcut(tr("Ctrl+G"));
170 }
171 
172 /*tools动作*/
173 void MainWindow::createToolsActions()
174 {
175     recalculateAction = new QAction(tr("&Recalculate"), this);
176     recalculateAction->setShortcut(tr("F9"));
177     recalculateAction->setStatusTip(tr("Recalculate all the "
178         "spreadsheet‘s formulas"));
179     
180     sortAction = new QAction(tr("&Sort..."), this);
181     sortAction->setStatusTip(tr("Sort the selected cells or all "
182         "the cells"));
183 }
184 
185 /*options动作*/
186 void MainWindow::createOptionsActions()
187 {
188     showGridAction = new QAction(tr("&Show Grid"), this);
189     showGridAction->setCheckable(true);        /*使动作可被选*/
190     showGridAction->setStatusTip(tr("Show or hide the "
191         "spreadsheet‘s grid"));
192     
193     autoRecalculateAction = new QAction(tr("Auto-Recalculate"), this);
194     autoRecalculateAction->setCheckable(true);    /*使动作可被选*/
195     autoRecalculateAction->setStatusTip(tr("Switch auto-"
196         "recalculate on or off"));
197 }
198 
199 /*help动作*/
200 void MainWindow::createHelpAction()
201 {
202     aboutAction = new QAction(tr("&About"), this);
203     aboutAction->setStatusTip(tr("Show the application‘s "
204         "About box"));
205         
206     aboutQtAction = new QAction(tr("About &Qt"), this);
207     aboutQtAction->setStatusTip(tr("Show the Qt library‘s "
208         "About box"));
209 }
210 
211 /*工具栏*/
212 void MainWindow::createToolBar()
213 {
214     fileToolBar = addToolBar(tr("&File"));
215     fileToolBar->addAction(newAction);
216     fileToolBar->addAction(openAction);
217     fileToolBar->addAction(saveAction);
218     
219     editToolBar = addToolBar(tr("&Edit"));
220     editToolBar->addAction(cutAction);
221     editToolBar->addAction(copyAction);
222     editToolBar->addAction(pasteAction);
223     editToolBar->addSeparator();
224     editToolBar->addAction(findAction);
225     editToolBar->addAction(goToCellAction);
226 }
227 
228 /*状态栏*/
229 void MainWindow::createStatusBar()
230 {
231     /* W999 作用:1.显示的内容。2.决定locationLabel的尺寸大小*/
232     locationLabel = new QLabel(" W999 ");
233     /*对齐方式:居中对齐*/
234     locationLabel->setAlignment(Qt::AlignHCenter);
235     /*最小大小为窗口部件的理想大小*/
236     locationLabel->setMinimumSize(locationLabel->sizeHint());
237     
238     formulaLabel = new QLabel;
239     /*缩进,文本与左侧边的偏移量*/
240     formulaLabel->setIndent(3);
241     
242     /*单元格定位指示器,伸展因子默认为0*/
243     statusBar()->addWidget(locationLabel);
244     /*单元格公式指示器,伸展因子为1*/
245     statusBar()->addWidget(formulaLabel, 1);
246 }
247 
248 void MainWindow::setCurrentFile(const QString &fileName)
249 {
250     currentFileName = fileName;
251     /*true:保存过和未保存有区别;false:无区别*/
252     setWindowModified(true);
253     
254     QString shownName = tr("Untitled");
255     
256     if (!currentFileName.isEmpty())
257     {
258         shownName = strippedName(currentFileName);    /*名字去掉路径*/        
259     }
260     
261     /*[*]:当WindowModified属性为true时,保存过的文件才没有星号*/
262     setWindowTitle(tr("%1[*] - %2").arg(shownName)
263                                    .arg("Spreadsheet"));
264 }
265 
266 /*去掉路径,只保留文件名*/
267 QString MainWindow::strippedName(const QString &fullFileName)
268 {
269     return QFileInfo(fullFileName).fileName();
270 }

技术分享

C++ GUI Qt4编程(10)-3.4spreadsheet

标签:路径   cell   ctr   作用   define   disk   actions   add   tool   

原文地址:http://www.cnblogs.com/seifguo/p/7101771.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!