void QAction::setMenu(QMenu *menu)
函数解释:Sets the menu contained by this action to the specified menu. Actions that contain menus can be used to create menu items with submenus, or inserted into toolbars to create buttons with popup menus.
翻译:设置action所包含的菜单。 包含菜单的action可用于创建带有子菜单的菜单项,或插入到工具栏中以创建带有弹出菜单的按钮。
示例:
//创建QAction
QAction *file_newAct = new QAction(tr("&New"), this);
QAction *file_openAct = new QAction(tr("&Open"), this);
QAction *file_saveAct = new QAction(tr("&Save"), this);
QAction *file_saveAsAct = new QAction(tr("Save &As"), this);
//创建菜单栏
QMenu *fileMenu = menuBar()->addMenu(tr("&FileM"));
fileMenu->addAction(file_newAct);
fileMenu->addAction(file_openAct);
fileMenu->addAction(file_saveAct);
fileMenu->addAction(file_saveAsAct);
//创建工具栏
QToolBar *toolbar = new QToolBar(this);
this->addToolBar(Qt::TopToolBarArea, toolbar);
QAction *fileAct = new QAction(tr("FileA"), this);
fileAct->setMenu(fileMenu);
toolbar->addAction(fileAct);
效果: