QT菜单项里面的子菜单做工具栏切换
#include "mainwindow.h" #include <QMenu> #include <QMenuBar> #include <QToolBar> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { QMenuBar *mBar = this->menuBar();//菜单栏 QMenu *menu = mBar->addMenu("文件");//菜单 QMenu *edit = mBar->addMenu("编辑"); QMenu *window = mBar->addMenu("窗体"); QAction *toolbartoggles_ = new QAction("工具栏切换") ;//工具栏切换菜单项 QAction *new_ = new QAction("新建"); QAction *open_ = new QAction("打开"); QAction *save_ = new QAction("保存"); menu->addAction(new_); menu->addAction(open_); menu->addAction(save_); QAction *brushtool_ = new QAction("画刷"); QAction *erase_ = new QAction("橡皮"); QAction *line_ = new QAction("线条"); edit->addAction(brushtool_); edit->addAction(erase_); edit->addAction(line_); QMenu *togglemenu = new QMenu(this);//切换菜单 用来做子菜单 //文件工具栏 QToolBar *filetools = new QToolBar(tr("文件工具栏")); filetools->setObjectName("filetoolsbar"); filetools->addAction(new_);//工具栏增加菜单项 filetools->addAction(open_); filetools->addAction(save_); togglemenu->addAction(filetools->toggleViewAction());//把文件工具栏增加到切换菜单里面 addToolBar(Qt::TopToolBarArea, filetools);//工具栏显示到顶部工具栏区域 //绘画工具栏 QToolBar *drawtools = new QToolBar("绘图工具栏"); drawtools->setObjectName("drawtoolsbar"); drawtools->addAction(brushtool_); drawtools->addAction(erase_); drawtools->addAction(line_); togglemenu->addAction(drawtools->toggleViewAction()); addToolBar(Qt::LeftToolBarArea, drawtools); toolbartoggles_->setMenu(togglemenu); window->addAction(toolbartoggles_); } MainWindow::~MainWindow() { }