You need to get a list of actions, and then uncheck each of them. The list of actions is simply the list of QAction
children of the menu - but that's if they don't belong to an action group. That would be, then:
void Class::method2() {
auto actions = std::as_const(ui_->my_menu->findChildren<QAction*>());
for (auto *action : actions)
action->setChecked(false);
}
If you want to find actions in a particular action group, you need to name the action group with a unique name, to refer to it later:
static const QString k_my_action_group{QLatin1String("my_action_group")};
void Class::method1() {
auto *my_action_group = new QActionGroup{this};
my_action_group->setObjectName(k_my_action_group);
...
}
void Class::method2() {
if (auto *group = findChild<QActionGroup*>(k_my_action_group))
if (auto *action = group->checkedAction())
action->setChecked(false);
}
If you have multiple groups, they can share the same name, and then you'd iterate them too:
void Class::method2() { // if somehow more than one action can be checked
auto groups = std::as_const(findChildren<QActionGroup*>(k_my_action_group));
for (auto *group : groups)
if (auto *action = group->checkedAction())
action->setChecked(false);
}
If your compiler doesn't implement std::as_const
yet, use qAsConst
instead. The const-casting is the unfortunate effect of the implicit shared design of Qt containers.
Object names referred to in multiple places should be used as string constants with symbolic names. This makes typos detectable at compile time, vs. at runtime.