QMenu *menu = new QMenu("MyMenu"); menu->addAction("Exit", qApp, SLOT(quit()));
QAction *newAction = new QAction("New", this); newAction->setShortcut(QKeySequence::New); menu->addAction(newAction);
QMenu *fileMenu = new QMenu("File"); QMenu *editMenu = new QMenu("Edit"); QMenu *subMenu = new QMenu("Submenu"); subMenu->addAction("Sub option 1"); subMenu->addAction("Sub option 2"); editMenu->addMenu(subMenu); // Add actions to fileMenu and editMenu menuBar()->addMenu(fileMenu); menuBar()->addMenu(editMenu);In this example, two QMenu objects "File" and "Edit" are created. "Submenu" is added as a submenu to "Edit" menu using addMenu() method. Two actions "Sub option 1" and "Sub option 2" are added to "Submenu". The menu bar is then updated with the two menus using addMenu() method of QMainWindow. Package library: Qt GUI module.