How to add check/Uncheck QAction in the context menu?

indira picture indira · Nov 22, 2012 · Viewed 8.8k times · Source

I have created a context menu in Qt and I need the items in the menu works in a checked/Unchecked manner so that it toggles for every click on the respective item. How to add this feature to the QAction just like shown below? enter image description here

Answer

Marcus picture Marcus · Mar 18, 2016

Here is the relevant section from the manual.

checkable : bool

This property holds whether the action is a checkable action.

A checkable action is one which has an on/off state. For example, in a word processor, a Bold toolbar button may be either on or off. An action which is not a toggle action is a command action; a command action is simply executed, e.g. file save. By default, this property is false.

In some situations, the state of one toggle action should depend on the state of others. For example, "Left Align", "Center" and "Right Align" toggle actions are mutually exclusive. To achieve exclusive toggling, add the relevant toggle actions to a QActionGroup with the QActionGroup::exclusive property set to true.

Access functions:

bool isCheckable() const

void setCheckable(bool)

Notifier signal:

void changed()