QMenu *menu = new QMenu(this); // create a new menu widget menu->addAction("Action 1"); // add a simple action to the menu QAction *action2 = new QAction("Action 2", this); // create a new action with a parent widget menu->addAction(action2); // add the action to the menu
QMenu *menu = new QMenu(this); QAction *openAction = menu->addAction("Open"); QAction *saveAction = menu->addAction("Save"); QAction *quitAction = menu->addAction("Quit"); connect(openAction, &QAction::triggered, this, &MainWindow::open); connect(saveAction, &QAction::triggered, this, &MainWindow::save); connect(quitAction, &QAction::triggered, QApplication::quit);In this example, we create a menu widget with three actions: Open, Save, and Quit. We then connect these actions to functions that will be executed when the user triggers them. For example, when the user clicks the Open action, the MainWindow::open function will be called. The package library for this function is Qt, which is included with many C++ development environments (such as Qt Creator) or can be installed separately.