风泥

导航

Qt 创建菜单

MainWindow.h 文件

private:

QMenu *fileMenu;

QAction *newAct;

protected:

void contextMenuEvent(QContextMenuEvent* event);

 

MainWindow.cpp  文件

/*添加菜单*/

 fileMenu=menuBar()->addMenu(tr("&File"));    //添加文件菜单

fileMenu->addAction(newAct);   //在“文件”菜单中添加 子菜单
fileMenu->addSeparator();    //添加分隔线
fileMenu->addAction(exitAct);
/*创建QAction*/

newAct = new QAction ( tr("&New"), this);       //创建QAction

newAct->setShortcuts(QKeySequence::New);   //设置快捷键

newAct->setStatusTip(tr("Create a new file"));   //设置状态栏

connect(newAct, SIGNAL(triggered()), this,SLOT(newFile()));    //设置信号槽   点击菜单时会发出triggered()信号

 

void MainWindow::newFile()

{

       ..........      //当点击New时,该完成的功能

}

 

/*创建上下文关联菜单*/

voidMainWindow::contextMenuEvent(QContextMenuEvent*event)

{
QMenu menu(this);
menu.addAction(cutAct);
menu.addAction(copyAct);
menu.addAction(pasteAct);
menu.exec(event->globalPos());   //鼠标跟随
}

posted on 2015-05-18 21:29  风泥  阅读(318)  评论(0)    收藏  举报