旅途

如果想飞得高,就该把地平线忘掉

QT入门 二 建立菜单

#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上.

posted on 2007-09-23 15:30 旅途 阅读(442) 评论(0)  编辑 收藏 引用 所属分类: Linux开发


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理