Good day!
You are doing everything right, but it’s enough to add something to make it work the way you want: use the QActionGroup class
For example:
void GameWidget::rightButtonPressEvent(QMouseEvent * ev) { QMenu* popupMenu = new QMenu(this); QActionGroup *actionGroup = new QActionGroup(popupMenu); connect(popupMenu, SIGNAL(aboutToHide()), SLOT(popupHidden())); actionGroup->addAction(this->firstAction); firstAction->setCheckable(true); // хотя бы одно действие должно быть активно actionGroup->addAction(this->secondAction); actionGroup->addAction(this->thirdAction); ............. actionGroup->setExclusive(true); // переводим группу действий в эксклюзивный режим popupMenu->addActions(actionGroup->actions()); // добавляем в меню все действия сразу popupMenu->exec(ev->globalPos()); return; }
In this case, only one action will be active (i.e. the property checked = true).
More details as always in the Qt help: QActionGroup class reference
UPD: is irrelevant, but I noticed a mistake in your code: potentially this is a memory leak (I'm really not sure, you need to watch the whole project and best of all under debugging). The point is that the instance of the QMenu class is created on the heap, but it will not be deleted! (although of course it will be removed along with the GameWidget ). Moreover, apparently, the above code is the implementation of the processing of a mouse click, that is, we get so many instances of the QMenu class in memory how many keystrokes were (which is probably not good). I would suggest creating an instance of QMenu, not on the heap, but on the stack , so that it is killed at once when it exits the handler. There will be no problems with the exec () method since according to the Qt help:
Executes this menu synchronously.
Given the above, I would rewrite your code like this:
void GameWidget::rightButtonPressEvent(QMouseEvent * ev) { QMenu popupMenu(this); QActionGroup *actionGroup = new QActionGroup(&popupMenu); connect(&popupMenu, SIGNAL(aboutToHide()), SLOT(popupHidden())); actionGroup->addAction(this->firstAction); firstAction->setCheckable(true); // хотя бы одно действие должно быть активно actionGroup->addAction(this->secondAction); actionGroup->addAction(this->thirdAction); ............. actionGroup->setExclusive(true); // переводим группу действий в эксклюзивный режим popupMenu.addActions(actionGroup->actions()); // добавляем в меню все действия сразу popupMenu.exec(ev->globalPos()); return; // здесь popupMenu будет удален, а вместе с ним и actionGroup }
Once again, I don’t know your requirements, and it may also be a solution to move the QMenu* popupMenu ad to members of the GameWidget class and create an instance of the menu in the constructor.
In general, whatever the case - success to you!