#ifndef Mainwindow_h#defineMainwindow_h#include<QMainWindow>#include<QMenu>#include<QMenuBar>#include<QStatusBar>classMainWindow: Publicqmainwindow{Q_object Public: MainWindow (Qwidget*parent =0); ~MainWindow (); Qmenu* menu[Ten]; Qaction* act[Ten]; Qmenubar*MenuBar; Qstatusbar*status; PublicSlots:voidTrigermenu (qaction*act);};#endif //Mainwindow_h
#include"mainwindow.h"#include<Qt>#include<QtDebug>Mainwindow::mainwindow (Qwidget*parent): Qmainwindow (parent) { This->resize ( -, -); This-Centralwidget (); Status=NewQstatusbar ( This); menu[0] =NewQmenu ("file"); menu[0]->addaction ("Edit"); menu[0]->addaction ("View"); menu[0]->addaction ("Tools"); act[0] =NewQaction ("New", This); act[0]->setshortcut (Qt::ctrl |qt::key_a); act[0]->setstatustip ("This is a new menu"); act[1] =NewQaction ("Open", This); act[1]->setcheckable (true); menu[1] =NewQmenu ("Save"); menu[1]->addaction (act[0]); menu[1]->addaction (act[1]); menu[2] =NewQmenu ("Print"); menu[2]->addaction ("Print Settings"); menu[2]->addmenu (menu[1]); MenuBar=NewQmenubar ( This); MenuBar->addmenu (menu[0]); MenuBar->addmenu (menu[2]); MenuBar->setgeometry (0,0, This->width (), -); Connect (menubar,signal (triggered (qaction*)), This, SLOT (Trigermenu (qaction*)));} MainWindow::~MainWindow () {}voidMainwindow::trigermenu (qaction*Act) { if(Act->text () = ="New") {qdebug ()<<"the new menu is pressed"; }}
" mainwindow.h " <QApplication>int main (intChar *argv[]) { qapplication a (argc, ARGV); MainWindow W; W.show (); return a.exec ();}
qt5-Control-qmenu,qmenubar-Menu Bar-Menu Hotkey-menu Check function