首页 > 代码库 > C++ GUI Qt4编程(07)-3.1menu
C++ GUI Qt4编程(07)-3.1menu
1. C++ GUI Qt4编程第三章,添加menu菜单。
2. mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> class QMenu; class QAction; class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(); private: /*菜单*/ 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; void createMenus(); void createActions(); void createFileActions(); void createEditActions(); void createToolsActions(); void createOptionsActions(); void createHelpAction(); }; #endif /*MAINWINDOW_H*/
3. mainwindow.cpp
#include <QMenu> #include <QMenuBar> #include <QAction> #include "mainwindow.h" MainWindow::MainWindow() { createActions(); createMenus(); } 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 = new QAction(tr("&New"), this); openAction = new QAction(tr("&Open"), this); saveAction = new QAction(tr("&Save"), this); saveAsAction = new QAction(tr("Save &As..."), this); exitAction = new QAction(tr("E&xit"), this); } /*edit动作*/ void MainWindow::createEditActions() { cutAction = new QAction(tr("Cu&t"), this); copyAction = new QAction(tr("&Copy"), this); pasteAction = new QAction(tr("&Paste"), this); deleteAction = new QAction(tr("&Delete"), this); selectRowAction = new QAction(tr("&Row"), this); selectColumnAction = new QAction(tr("&Column"), this); selectAllAction = new QAction(tr("&All"), this); findAction = new QAction(tr("&Find..."), this); goToCellAction = new QAction(tr("&Go to Cell..."), this); } /*tools动作*/ void MainWindow::createToolsActions() { recalculateAction = new QAction(tr("&Recalculate"), this); sortAction = new QAction(tr("&Sort..."), this); } /*options动作*/ void MainWindow::createOptionsActions() { showGridAction = new QAction(tr("&Show Grid"), this); autoRecalculateAction = new QAction(tr("Auto-Recalculate"), this); } /*help动作*/ void MainWindow::createHelpAction() { aboutAction = new QAction(tr("&About"), this); aboutQtAction = new QAction(tr("About &Qt"), this); }
4. main.cpp
#include <QApplication> #include "mainwindow.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); MainWindow *win = new MainWindow; win->show(); return app.exec(); }
C++ GUI Qt4编程(07)-3.1menu
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。