#ifndef WIN_H
#define WIN_H
#include <qmainwindow.h>
class QAction;
class QPopupMenu;
class Mywin: public QMainWindow
{
Q_OBJECT
public:
Mywin(QWidget *parent=0,const char * name=0);
private:
void createActions();
void createMenus();
private slots:
void newFile();
private:
QAction *newAct;
QPopupMenu *fileMenu;
};
#endif
#include "win.h"
#include <qapplication.h>
#include <qaction.h>
#include <qmessagebox.h>
#include <qpopupmenu.h>
#include <qmenubar.h>
Mywin::Mywin(QWidget *parent,const char * name):QMainWindow(parent,name)
{
createActions();
createMenus();
}
void Mywin::createActions()
{
newAct=new QAction(NULL,tr("&New"),0,this);
// newAct->setStatusTip(tr("Create a new file"));
connect(newAct,SIGNAL(activated()),this,SLOT(newFile()));
}
void Mywin::createMenus()
{#include "win.h"
#include <qapplication.h>
#include <qaction.h>
#include <qmessagebox.h>
#include <qpopupmenu.h>
#include <qmenubar.h>
Mywin::Mywin(QWidget *parent,const char * name):QMainWindow(parent,name)
{
createActions();
createMenus();
}
void Mywin::createActions()
{
newAct=new QAction(NULL,tr("&New"),0,this);
connect(newAct,SIGNAL(activated()),this,SLOT(newFile()));
}
void Mywin::createMenus()
{
fileMenu=new QPopupMenu(this);
newAct->addTo(fileMenu);
menuBar()->insertItem(tr("&File"),fileMenu);
}
void Mywin::newFile()
{
QMessageBox::warning(this,tr("Hello"),tr("Hello \n World!"),QMessageBox::Yes|QMessageBox::Default,QMessageBox::No,QMessageBox::Cancel|QMessageBox::Escape);
}
#include <qapplication.h>
#include "win.h"
int main(int argc,char* argv[])
{
QApplication app(argc,argv);
Mywin *mywin=new Mywin;
app.setMainWidget(mywin);
mywin->show();
return app.exec();
}
1
1 newAct=new QAction(NULL,tr("&New"),0,this);
connect(newAct,SIGNAL(activated()),this,SLOT(newFile()));
建立一个菜单项,并将点击信号连接到槽newFile()
2 fileMenu=new QPopupMenu(this);
newAct->addTo(fileMenu);
建立一个菜单
3 menuBar()返回一个MenuBar
menuBar()->insertItem(tr("&File"),fileMenu);
将菜单添加到MenuBar上.