//Creating a menu for a QAction QMenu *fileMenu = new QMenu(tr("File"), this); QAction *openAct = new QAction(tr("&Open"), this); fileMenu->addAction(openAct); //Retrieving the QAction from the menu QAction *action = fileMenu->menuAction();
//Creating a submenu with multiple QAction items QMenu *editMenu = new QMenu(tr("Edit"), this); QMenu *fontMenu = new QMenu(tr("Font"), this); editMenu->addMenu(fontMenu); QAction *boldAct = new QAction(tr("Bold"), this); fontMenu->addAction(boldAct); QAction *italicAct = new QAction(tr("Italic"), this); fontMenu->addAction(italicAct); QAction *underlineAct = new QAction(tr("Underline"), this); editMenu->addAction(underlineAct); //Retrieving the QAction from the submenu QAction *action = fontMenu->menuAction();This example creates a submenu for the "Edit" menu, containing three QAction items for bold, italic, and underline. The menuAction() function is used to retrieve the fontMenu QAction, which can be connected to a slot function to apply the selected font style. Package Library: Qt GUI Framework.