创建一个Qt Widgets Application项目
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QAction>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
void CreateActons();
void CreateMenu();
QAction *NewFileAction;
QAction *OpenFileAction;
QAction *SaveFileAction;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->CreateActons();
this->CreateMenu();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::CreateActons()
{
// this->NewFileAction = new QAction(QIcon("Icons/new.png"), trUtf8("新建"), this);
this->NewFileAction = new QAction(trUtf8("新建"), this);
this->NewFileAction->setShortcut(trUtf8("Ctrl+N"));
this->NewFileAction->setStatusTip(trUtf8("新建一个文件"));
// this->OpenFileAction = new QAction(QIcon("Icons/open.png"), trUtf8("打开"), this);
this->OpenFileAction = new QAction(trUtf8("打开"), this);
this->OpenFileAction->setShortcut(trUtf8("Ctrl+O"));
this->OpenFileAction->setStatusTip(trUtf8("打开一个文件"));
// this->SaveFileAction = new QAction(QIcon("Icons/save.png"), trUtf8("保存"), this);
this->SaveFileAction = new QAction(trUtf8("保存"), this);
this->SaveFileAction->setShortcut(trUtf8("Ctrl+S"));
this->SaveFileAction->setStatusTip(trUtf8("保存文件"));
}
void MainWindow::CreateMenu()
{
QList<QAction*> FileActionsList;
FileActionsList << NewFileAction
<< OpenFileAction
<< SaveFileAction;
ui->menuBar->addMenu(trUtf8("文件"))->addActions(FileActionsList);
}