QAction* action = new QAction("Toggle", this); action->setCheckable(true); action->setChecked(false); connect(action, &QAction::triggered, this, &MyWidget::onActionTriggered);
QMenu* menu = new QMenu("File", this); QAction* action = new QAction("Save", this); action->setShortcut(QKeySequence::Save); action->setCheckable(true); action->setChecked(true); menu->addAction(action);In this example, a new QMenu object is created with the text "File" and added to a widget. Then, a new QAction object is created with the text "Save" and assigned the shortcut key sequence QKeySequence::Save. The setCheckable function is called to enable the action to be checked and unchecked. Then, the setChecked function sets the initial state of the action to checked. Finally, the action is added to the menu using the addAction function. Package Library: Qt Core Library