mainwindow.h:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> class MainWindow : public QMainWindow { Q_OBJECT public slots: void foo(); public: MainWindow(QWidget *parent = 0); ~MainWindow(); private: void QMenBar_init(); }; #endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h" #include<QMenuBar> #include<QMenu> #include<QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { QMenBar_init(); } void MainWindow::QMenBar_init() { this->resize(600,480); QMenuBar * qm =this->menuBar(); QMenu * wen_jian = new QMenu("文件(&F)",qm); //快捷方式"Alt+F" QMenu*bian_ji = new QMenu("编辑(&E)",qm); QMenu * gou_jian= new QMenu("构建(&B)",qm); QMenu* tiao_shi = new QMenu("调试(&D)",qm); wen_jian->addAction("新建文件或项目(N...)",this,SLOT(foo()),QKeySequence("Ctrl+N")); wen_jian->addSeparator(); //添加分割线 QMenu* zui_file=wen_jian->addMenu("最近访问的文件(F)"); zui_file->addAction(" "); zui_file->addSeparator(); zui_file->addAction("清除菜单"); bian_ji->addAction(QIcon("C:/Users/admin/Desktop/QT_Project/untitled/1.png"),"Undo"); tiao_shi->addAction("default")->setCheckable(true); qm->addMenu(wen_jian); qm->addMenu(bian_ji); qm->addMenu(gou_jian); qm->addMenu(tiao_shi); } void MainWindow::foo() { qDebug()<<"打开成功"<<endl; } MainWindow::~MainWindow() { }
main.cpp:
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
效果: