QMenu* menu = new QMenu("File", this); QAction* action = new QAction("Open", this); menu->addAction(action); menu->insertAction(menu->actions().at(0), new QAction("New", this)); // Inserts "New" at the top of the menu
QMenu* menu = new QMenu("Options", this); menu->insertAction(nullptr, new QAction("Option 1", this)); menu->insertAction(menu->actions().at(0), new QAction("Option 2", this)); menu->insertAction(menu->actions().at(1), new QAction("Option 3", this));In this example, we create a new QMenu object named "Options". Then we add three QAction objects to it using the insertAction method, with the second and third actions being inserted at specific positions relative to the first action. Package/library: Qt.