1

I have a toolbar or there are several QActions. I would like to change the color of the QAction when it is selected so that the user can see the chosen action.

The problem is that QAction is not a QWidget so we can not use stylesheet. Is there a way to get around this problem? Or would it be better to declare QPushButton instead of QAction in my toolbar?

I would like, for example, that if I click on a QAction it is pushed against the others. I have 6 QAction declared in my toolbar.

Exa
  • 4,020
  • 7
  • 43
  • 60
  • 2
    In your [*QToolBar*](http://doc.qt.io/qt-5/qtoolbar.html), you can use [*QToolButton*](http://doc.qt.io/qt-5/qabstractbutton.html) and `setCheckable(true)`, in this case, it is a QWidget so you can use style sheet – thibsc Jul 04 '18 at 08:25
  • Thanks @Thibaut B. it works. –  Jul 04 '18 at 08:41

1 Answers1

1

You have to checkeable the QAction with:

your_action->setCheckable(true)

or using Qt Designer

enter image description here

and then set qss with QToolButton:

QToolButton:checked {background-color: red; }

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thank you @eyllanesc it works By cons I do not know why there is a negative vote –  Jul 04 '18 at 08:38
  • @Tom13000 I do not really care about the downvote, I just made a comment. :) – eyllanesc Jul 04 '18 at 08:39
  • How can I access the action on code? My action is not appearing under `ui->` nor under `ui->mainToolBar` (which is where it appears on UI). Where else should it be? I'm using Qt Creator 4.8.2 on Debian 10. – Rodrigo Apr 18 '21 at 02:55