I'm adding actions to a QToolBar only using icons and an empty text, and I want to change each action style when it is triggered (specifically, changing its border color):
toolbar = new QToolBar;
action1 = toolbar->addAction(my_icon1, "");
action2 = toolbar->addAction(my_icon2, "");
QObject::connect(action1, &QAction::triggered, [this]{
// change border color of action1
// unset border color of action2
});
QObject::connect(action2, &QAction::triggered, [this]{
// change border color of action2
// unset border color of action1
});
But since a QIcon is not a widget (not a QAction of course), I don't know where to set the style of a specific action, and QAction::associatedWidget() returns the QToolBar widget and not the associated button that owns the icon.
I'm using only C++ code, without QML or ui files.
Since
QAction::parentWidgetandQAction::associatedWidgetsboth contain theQToolBarinstead of the actual action widget, I didn't give initial credit toQToolBar::widgetForAction(I thought it would be some kind of convenient function for theQAction::methods above). But it deserves it, because it returns the actual widget for that action, as the function name says: