int id = menuBar->insertItem("EXIT");
menuBar->connectItem(id,this,SLOT(exit()));
connect(menuBar, SIGNAL(activated(int),this,SLOT(doMenuItem(int))))
connect(menuBar, SIGNAL(highlighted(int),this,SLOT(doMenuItem(int))))
void MainWindow::createMenus(){ serviceForPrint = new QPopupMenu(this); imageScreen = new QPopupMenu(this); consoleMnu = new QPopupMenu(this); helpMnu = new QPopupMenu(this); QMenuBar *menuBar = new QMenuBar(this); menuBar->insertItem(tr("&Сервис для печати"), serviceForPrint); menuBar->insertItem(tr("Образ &экрана"), imageScreen); menuBar->insertItem(tr("&Консоль"), consoleMnu); menuBar->insertItem(tr("&Помощь"), helpMnu); int idExit = menuBar->insertItem("&Выход); idExit = menuBar->connectItem(idExit, this, SLOT(exit())); connect(menuBar, SIGNAL(highlighted(int)), this, SLOT(noActivated(int))); }
class MainWindow : public QMainWindow{ Q_OBJECTpublic: MainWindow(); bool eventFilter( QObject* obj, QEvent* e );public slots: void onExit();private: bool mousePressed;};MainWindow::MainWindow() : QMainWindow(){ mousePressed = false; QPopupMenu* fileMenu = new QPopupMenu( this ); fileMenu->insertItem(tr("&Open")); fileMenu->insertItem(tr("&Save")); menuBar()->insertItem(tr("&File"), fileMenu ); menuBar()->insertItem( tr("&Exit"), this, SLOT(onExit()) ); menuBar()->installEventFilter( this );}bool MainWindow::eventFilter( QObject* obj, QEvent* e ){ if ( obj == menuBar() ) { if ( e->type() == QEvent::MouseButtonPress && ((QMouseEvent*)e)->button() == LeftButton ) mousePressed = true; if ( e->type() == QEvent::MouseButtonRelease && ((QMouseEvent*)e)->button() == LeftButton ) mousePressed = false; } return false;}void MainWindow::onExit() { if (mousePressed) qApp->quit(); }