相信自己,只有不想做的,没有做不到的。
分类: LINUX
2014-07-19 01:36:33
class QMenu;
在pro中添加 QT += widgets
QMenu类作为菜单栏,QAction类作为点击菜单栏的下拉菜单使用 QMenu类对象的addaction(QAction类的对象)来添加
具体代码如下:
//main.c---------------------------------------------------------------------
#include "mainwindow.h"#includeint main(int argc, char *argv[]){//初始化qrc资源Q_INIT_RESOURCE(application);QApplication a(argc, argv);a.setOrganizationName("Qtporject");//给窗口添加app namea.setApplicationName("Application hello Example");MainWindow w;w.show();
return a.exec();}
//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();//文本框QPlainTextEdit *textEdit;//file菜单QMenu * fileMenu;//定义了file的下拉菜单为 new open save saveAS exitQAction *newAct;QAction *open;QAction *Save;QAction *saveAs;QAction *exit;};#endif // MAINWINDOW_H
//mainwindow.cpp---------------------------------------------------------------------
#include "mainwindow.h"#include//#include
MainWindow::MainWindow(){//创建一个文本编辑器textEdit = new QPlainTextEdit;//设置文本编辑器为中心位置setCentralWidget(textEdit);//首先创建file下拉的菜单栏createActions();
//再来创建菜单目录,把下拉菜单的内容addaction加入到menu中createMenus();
}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())); //New点击事件调用testopen = 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())); //点击Open事件调用testSave = 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())); //点击Save事件调用testsaveAs = new QAction( tr("&SaveAs"), this);saveAs->setShortcuts(QKeySequence::SaveAs);newAct->setStatusTip(tr("SaveAs a new file"));connect(saveAs, SIGNAL(triggered()), this, SLOT(test())); //saveAS点击事件调用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())); //点击exit事件调用test}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);}实验效果如下:这个一个简单菜单栏以及它的下拉菜单做好了,如果想要点击下拉菜单去打开,保存,退出即可在各自所对应的槽函数中去实现。