相信自己,只有不想做的,没有做不到的。
分类: LINUX
2014-07-21 22:11:14
QMenu * editMenu;
QMenu * helpMenu;下拉菜单的类:
//定义edit的下拉菜单cut copy past
QAction *cut;
QAction *copy;
QAction *past;
//定义help的下拉菜单about aboutQt
QAction *about;
QAction *aboutQt;edit与help使用方法为:
QToolBar *fileToolBar;QToolBar *editToolBar;使用方法为:fileToolBar = addToolBar(tr("File"));fileToolBar->addAction(newAct);fileToolBar->addAction(open);fileToolBar->addAction(Save);editToolBar = addToolBar(tr("Edit"));editToolBar->addAction(cut);editToolBar->addAction(copy);editToolBar->addAction(past);
实现代码入如下://mainwindow.h#ifndef MAINWINDOW_H#define MAINWINDOW_H#include#include//包含QPlainTextEdit QAction QMenu 才能够使用这个类class QPlainTextEdit;class QAction;class QMenu;class MainWindow : public QMainWindow{Q_OBJECTpublic:
explicit MainWindow();~MainWindow();private slots:void test();private:
void createActions();void createMenus();void createToolBars();、//文本框QPlainTextEdit *textEdit;//file菜单QMenu * fileMenu;QMenu * editMenu;QMenu * helpMenu;QToolBar *fileToolBar;QToolBar *editToolBar;//定义了file的下拉菜单为 new open save saveAS exitQAction *newAct;QAction *open;QAction *Save;QAction *saveAs;QAction *exit;//定义edit的下拉菜单cut copy pastQAction *cut;QAction *copy;QAction *past;//定义help的下拉菜单about aboutQtQAction *about;QAction *aboutQt;};#endif // MAINWINDOW_H//mainwindow.cpp代码如下#include "mainwindow.h"
MainWindow::MainWindow()
{//创建一个文本编辑器textEdit = new QPlainTextEdit;//设置文本编辑器为中心位置setCentralWidget(textEdit);//首先创建file下拉的菜单栏createActions();
//再来创建菜单目录,把下拉菜单的内容addaction加入到menu中createMenus();
createToolBars();
}MainWindow::~MainWindow(){}
void MainWindow::test(){
}//file下拉菜单 名字为 new open Save saveAs exit new.png为图标 添加了快捷键 以及添加点击这个下拉菜单所条用的槽函数,这里使用test作为测试使用void MainWindow::createActions(){newAct = new QAction(QIcon(":/image/new.png"), tr("&New"), this);newAct->setShortcuts(QKeySequence::New);newAct->setStatusTip(tr("Create a new file"));connect(newAct, SIGNAL(triggered()), this, SLOT(test()));
open = new QAction(QIcon(":/image/open.png"), tr("&Open"), this);open->setShortcuts(QKeySequence::Open);open->setStatusTip(tr("open a new file"));connect(open, SIGNAL(triggered()), this, SLOT(test()));Save = new QAction(QIcon(":/image/save.png"), tr("&Save"), this);Save->setShortcuts(QKeySequence::Save);Save->setStatusTip(tr("Save a new file"));connect(Save, SIGNAL(triggered()), this, SLOT(test()));
saveAs = new QAction( tr("&SaveAs"), this);saveAs->setShortcuts(QKeySequence::SaveAs);newAct->setStatusTip(tr("SaveAs a new file"));connect(saveAs, SIGNAL(triggered()), this, SLOT(test()));exit = new QAction( tr("&exit"), this);exit->setShortcuts(QKeySequence::Close);exit->setStatusTip(tr("exit a new file"));connect(exit, SIGNAL(triggered()), this, SLOT(close()));cut = new QAction(QIcon(":/image/cut.png"), tr("&Cut"), this);cut->setShortcuts(QKeySequence::Cut);cut->setStatusTip(tr("Save a new file"));connect(cut, SIGNAL(triggered()), this, SLOT(test()));
copy = new QAction(QIcon(":/image/copy.png"), tr("&Copy"), this);saveAs->setShortcuts(QKeySequence::Copy);copy->setStatusTip(tr("SaveAs a new file"));connect(copy, SIGNAL(triggered()), this, SLOT(test()));
past = new QAction(QIcon(":/image/paste.png"), tr("&Paste"),this);past->setShortcuts(QKeySequence::Paste);past->setStatusTip(tr("exit a new file"));connect(past, SIGNAL(triggered()), this, SLOT(close()));about = new QAction(tr("&about"), this);about->setStatusTip(tr("SaveAs a new file"));connect(about, SIGNAL(triggered()), this, SLOT(test()));aboutQt = new QAction(tr("&aboutQt"),this);aboutQt->setStatusTip(tr("exit a new file"));connect(aboutQt, SIGNAL(triggered()), this, SLOT(close()));}void MainWindow::createToolBars(){fileToolBar = addToolBar(tr("File"));fileToolBar->addAction(newAct);fileToolBar->addAction(open);fileToolBar->addAction(Save);editToolBar = addToolBar(tr("Edit"));editToolBar->addAction(cut);editToolBar->addAction(copy);editToolBar->addAction(past);}void MainWindow::createMenus(){//创建一个name为file的菜单栏fileMenu = menuBar()->addMenu(tr("&File"));//在这个菜单栏添加action即下拉菜单fileMenu->addAction(newAct);fileMenu->addAction(open);fileMenu->addAction(Save);fileMenu->addAction(saveAs);//添加一个分割器fileMenu->addSeparator();//推出下拉菜单fileMenu->addAction(exit);//创建一个name为edit的菜单栏editMenu = menuBar()->addMenu(tr("&Edit"));//在这个菜单栏添加action即下拉菜单editMenu->addAction(cut);editMenu->addAction(copy);editMenu->addAction(past);//创建一个name为help的菜单栏\helpMenu = menuBar()->addMenu(tr("&Help"));//在这个菜单栏添加action即下拉菜单helpMenu->addAction(about);helpMenu->addAction(aboutQt);}//main.cpp代码如下:
#include "mainwindow.h"#includeint main(int argc, char *argv[]){QApplication a(argc, argv);MainWindow w;w.show();
return a.exec();}代码就是以上这些,如果要给open,save,cut,copy,paste添加图标的话,即可添加下载图片添加到qrc文件中这里没有对每个对每个菜单的下拉菜单进行实现,只是链接了简单的槽函数,留下了接口,实验效果如下:菜单栏QMenu,下拉菜单QAction,工具栏QToolBar的使用就学习到这里,这些控件中还有很多功能,在实际应用中慢慢去发现。