#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow>
#include <QMenu>
#include <QMenuBar>
#include <QStatusBar> class MainWindow : public QMainWindow
{
Q_OBJECT public:
MainWindow(QWidget *parent = );
~MainWindow(); QMenu* menu[];
QAction* act[];
QMenuBar* menuBar ;
QStatusBar* status ;
public slots:
void trigerMenu(QAction* act);
}; #endif // MAINWINDOW_H
#include "mainwindow.h"
#include <Qt>
#include <QtDebug> MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
this->resize(,);
this->centralWidget(); status = new QStatusBar(this); menu[] = new QMenu("文件");
menu[]->addAction("编辑");
menu[]->addAction("查看");
menu[]->addAction("工具"); act[] = new QAction("新建",this);
act[]->setShortcut(Qt::CTRL | Qt::Key_A );
act[]->setStatusTip("这是一个新建菜单"); act[] = new QAction("打开",this);
act[]->setCheckable(true); menu[] = new QMenu("保存");
menu[]->addAction(act[]);
menu[]->addAction(act[]); menu[] = new QMenu("打印");
menu[]->addAction("打印设置");
menu[]->addMenu(menu[]); menuBar = new QMenuBar(this);
menuBar->addMenu(menu[]);
menuBar->addMenu(menu[]);
menuBar->setGeometry(,,this->width(),); connect(menuBar,SIGNAL(triggered(QAction*)),this,SLOT(trigerMenu(QAction*)));
} MainWindow::~MainWindow()
{ } void MainWindow::trigerMenu(QAction* act)
{
if(act->text() == "新建")
{
qDebug()<<"新建菜单被按下";
}
}
#include "mainwindow.h"
#include <QApplication> int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show(); return a.exec();
}