MainWindow.h


#ifndef MAINWINDOW_H #define MAINWINDOW_H  #include<QMainWindow> #include<QMessageBox>   class MainWindow : public QMainWindow{     Q_OBJECT public:     MainWindow();  private slots:     void fExit();     void iInfo();  private:     void createMenus();     void createActions();      QAction *exit;     QAction *info;     QMenu *file;     QMenu *about;     QMessageBox *msgBox; };  #endif // MAINWINDOW_H



MainWindow.cpp


#include<QtGui> #include"MainWindow.h"  MainWindow::MainWindow(){     QWidget *widget=new QWidget;     setCentralWidget(widget);      QWidget *topfiller=new QWidget;     topfiller->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);      QWidget *bottomFiller=new QWidget;     bottomFiller->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);      createActions();     createMenus();      QVBoxLayout *layout=new QVBoxLayout;     layout->addWidget(topfiller);     layout->addWidget(bottomFiller);     widget->setLayout(layout);      setWindowTitle(tr("Main Window"));     resize(512,480);  }  void MainWindow::createActions(){     exit=new QAction(tr("Exit"),this);     exit->setShortcut(QKeySequence::Close);     connect(exit,SIGNAL(triggered()),this,SLOT(fExit()));      info =new QAction(tr("Info"),this);     info->setShortcut(QKeySequence::HelpContents);     connect(info,SIGNAL(triggered()),this,SLOT(iInfo()));  }  void MainWindow::createMenus(){     file=menuBar()->addMenu(tr("File"));     file->addAction(exit);      about=menuBar()->addMenu(tr("About"));     about->addAction(info); }  void MainWindow::fExit(){     MainWindow::close(); }  void MainWindow::iInfo(){     msgBox=new QMessageBox;     msgBox->setText(tr("hello world"));     msgBox->setIcon(QMessageBox::Question);     msgBox->setStandardButtons(QMessageBox::Ok);     msgBox->exec(); }



Main.cpp


#include<QApplication> #include"MainWindow.h"   int main(int argc,char* argv[]){     QApplication app(argc,argv);     MainWindow wnd;     wnd.show();     app.exec();  }

运行效果: