C++ GUI Qt4编程(08)-3.2spreadsheet-resource
1. C++ GUI Qt4编程第三章,图片使用资源机制法。
2. 步骤:
2-1. 在resource文件夹下,新建images文件,存放图片。
2-2. 新建spreadsheet.qrc文件,并在.pro文件中添加代码:
RESOURCES = spreadsheet.qrc
2-3. 打开.qrc文件,写入images文件中图片名称。
3. mainwindow.h
1 /**/ 2 #ifndef MAINWINDOW_H 3 #define MAINWINDOW_H 4 5 #include <QMainWindow> 6 7 class QMenu; 8 class QAction; 9 10 class MainWindow : public QMainWindow 11 { 12 Q_OBJECT 13 14 public: 15 MainWindow(); 16 17 private: 18 /*菜单*/ 19 QMenu *fileMenu; 20 QMenu *editMenu; 21 QMenu *toolsMenu; 22 QMenu *optionsMenu; 23 QMenu *helpMenu; 24 25 /*File动作*/ 26 QAction *newAction; 27 QAction *openAction; 28 QAction *saveAction; 29 QAction *saveAsAction; 30 QAction *exitAction; 31 32 /*Edit动作*/ 33 QAction *cutAction; 34 QAction *copyAction; 35 QAction *pasteAction; 36 QAction *deleteAction; 37 QMenu *selectSubMenu; 38 QAction *selectRowAction; 39 QAction *selectColumnAction; 40 QAction *selectAllAction; 41 QAction *findAction; 42 QAction *goToCellAction; 43 44 /*Tools动作*/ 45 QAction *recalculateAction; 46 QAction *sortAction; 47 48 /*Options动作*/ 49 QAction *showGridAction; 50 QAction *autoRecalculateAction; 51 52 /*Help动作*/ 53 QAction *aboutAction; 54 QAction *aboutQtAction; 55 56 void createMenus(); 57 void createActions(); 58 void createFileActions(); 59 void createEditActions(); 60 void createToolsActions(); 61 void createOptionsActions(); 62 void createHelpAction(); 63 }; 64 65 #endif /*MAINWINDOW_H*/
4. mainwindow.cpp
1 /**/ 2 #include <QMenu> 3 #include <QMenuBar> 4 #include <QAction> 5 #include "mainwindow.h" 6 7 MainWindow::MainWindow() 8 { 9 createActions(); 10 createMenus(); 11 } 12 13 void MainWindow::createMenus() 14 { 15 /*file menu*/ 16 fileMenu = menuBar()->addMenu(tr("&File")); 17 fileMenu->addAction(newAction); 18 fileMenu->addAction(openAction); 19 fileMenu->addAction(saveAction); 20 fileMenu->addAction(saveAsAction); 21 fileMenu->addSeparator(); 22 fileMenu->addAction(exitAction); 23 24 /*edit menu*/ 25 editMenu = menuBar()->addMenu(tr("&Edit")); 26 editMenu->addAction(cutAction); 27 editMenu->addAction(copyAction); 28 editMenu->addAction(pasteAction); 29 editMenu->addAction(deleteAction); 30 selectSubMenu = editMenu->addMenu(tr("&Select")); 31 selectSubMenu->addAction(selectRowAction); 32 selectSubMenu->addAction(selectColumnAction); 33 selectSubMenu->addAction(selectAllAction); 34 editMenu->addSeparator(); 35 editMenu->addAction(findAction); 36 editMenu->addAction(goToCellAction); 37 38 /*tools menu*/ 39 toolsMenu = menuBar()->addMenu(tr("&Tools")); 40 toolsMenu->addAction(recalculateAction); 41 toolsMenu->addAction(sortAction); 42 43 /*option menu*/ 44 optionsMenu = menuBar()->addMenu(tr("&Option")); 45 optionsMenu->addAction(showGridAction); 46 optionsMenu->addAction(autoRecalculateAction); 47 48 /*间隔器*/ 49 menuBar()->addSeparator(); 50 51 /*help menu*/ 52 helpMenu = menuBar()->addMenu(tr("&Help")); 53 helpMenu->addAction(aboutAction); 54 helpMenu->addAction(aboutQtAction); 55 } 56 57 void MainWindow::createActions() 58 { 59 createFileActions(); 60 createEditActions(); 61 createToolsActions(); 62 createOptionsActions(); 63 createHelpAction(); 64 } 65 66 /*file动作*/ 67 void MainWindow::createFileActions() 68 { 69 /*newAction*/ 70 newAction = new QAction(tr("&New"), this); 71 newAction->setIcon(QIcon(":/images/filenew.png")); 72 73 /*openAction*/ 74 openAction = new QAction(tr("&Open"), this); 75 openAction->setIcon(QIcon(":/images/fileopen.png")); 76 77 /*saveAction*/ 78 saveAction = new QAction(tr("&Save"), this); 79 saveAction->setIcon(QIcon(":/images/filesave.png")); 80 81 /*saveAsAction*/ 82 saveAsAction = new QAction(tr("Save &As..."), this); 83 saveAsAction->setIcon(QIcon(":/images/filesaveas.png")); 84 85 /*exitAction */ 86 exitAction = new QAction(tr("E&xit"), this); 87 } 88 89 /*edit动作*/ 90 void MainWindow::createEditActions() 91 { 92 /*cutAction*/ 93 cutAction = new QAction(tr("Cu&t"), this); 94 cutAction->setIcon(QIcon(":/images/editcut.png")); 95 96 /*copyAction*/ 97 copyAction = new QAction(tr("&Copy"), this); 98 copyAction->setIcon(QIcon(":/images/editcopy.png")); 99 100 /*pasteAction*/ 101 pasteAction = new QAction(tr("&Paste"), this); 102 pasteAction->setIcon(QIcon(":/images/editpaste.png")); 103 104 /*deleteAction*/ 105 deleteAction = new QAction(tr("&Delete"), this); 106 deleteAction->setIcon(QIcon(":/images/editdelete.png")); 107 108 selectRowAction = new QAction(tr("&Row"), this); 109 selectColumnAction = new QAction(tr("&Column"), this); 110 selectAllAction = new QAction(tr("&All"), this); 111 112 /*findAction*/ 113 findAction = new QAction(tr("&Find..."), this); 114 findAction->setIcon(QIcon(":/images/editfind.png")); 115 116 /*goToCellAction*/ 117 goToCellAction = new QAction(tr("&Go to Cell..."), this); 118 goToCellAction->setIcon(QIcon(":/images/editgotocell")); 119 } 120 121 /*tools动作*/ 122 void MainWindow::createToolsActions() 123 { 124 recalculateAction = new QAction(tr("&Recalculate"), this); 125 sortAction = new QAction(tr("&Sort..."), this); 126 } 127 128 /*options动作*/ 129 void MainWindow::createOptionsActions() 130 { 131 showGridAction = new QAction(tr("&Show Grid"), this); 132 autoRecalculateAction = new QAction(tr("Auto-Recalculate"), this); 133 } 134 135 /*help动作*/ 136 void MainWindow::createHelpAction() 137 { 138 aboutAction = new QAction(tr("&About"), this); 139 aboutQtAction = new QAction(tr("About &Qt"), this); 140 }
5. main.cpp
1 #include <QApplication> 2 #include "mainwindow.h" 3 4 int main(int argc, char *argv[]) 5 { 6 QApplication app(argc, argv); 7 8 MainWindow *win = new MainWindow; 9 win->show(); 10 11 return app.exec(); 12 }
6. spreadsheet.qrc
1 <RCC> 2 <qresource> 3 <file>images/editcopy.png</file> 4 <file>images/editcut.png</file> 5 <file>images/editdelete.png</file> 6 <file>images/editfind.png</file> 7 <file>images/editgotocell.png</file> 8 <file>images/editpaste.png</file> 9 <file>images/filenew.png</file> 10 <file>images/fileopen.png</file> 11 <file>images/filesave.png</file> 12 <file>images/filesaveas.png</file> 13 </qresource> 14 </RCC>