// Creating a menu bar and adding a menu with QMenu QMenuBar *menuBar = new QMenuBar(this); QMenu *fileMenu = new QMenu("File", this); menuBar->addMenu(fileMenu);
// Adding an existing QMenu to the menu bar QMenuBar *menuBar = new QMenuBar(this); QMenu *fileMenu = new QMenu("File", this); menuBar->addMenu(fileMenu); QMenu *editMenu = new QMenu("Edit", this); menuBar->addMenu(editMenu); // ... // Some code later, we can add an existing QMenu to the menu bar QMenu *helpMenu = new QMenu("Help", this); menuBar->addMenu(helpMenu);In this example, we first create two menus and add them to the menu bar using addMenu(). Then, later in the code, we create a new menu and add it to the menu bar using the same function. The QMenuBar class is defined in the QtWidgets package library in the Qt C++ framework.