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