首页 > 代码库 > 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