tags:

views:

92

answers:

2

Is it possible to have a custom icon displayed for a QAction when it is disabled? E.g. display icon A when the tool is enabled and icon B when the tool is disabled.

A: 

Connect a slot to signal QAction::changed(), then set the icon there if the action is enabled or disabled. (You could do this by subclassing QAction and add the slot in the subclass, connecting it in the constructor).

Cătălin Pitiș
+1  A: 

When creating a QAction, you pass it a QIcon. Although I haven't tried this myself, I've noticed that QIcon has a function void QIcon::addPixmap ( const QPixmap & pixmap, Mode mode = Normal, State state = Off ). The Mode can be one of Normal, Disabled, Active, or Selected. Thus, I presume something like this would work:

QPixmap enabled_icon( "enabled.png" );
QPixmap disabled_icon( "disabled.png" );
QIcon icon( enabled_icon );
icon.addPixmap( disabled_icon, QIcon::Disabled );
QAction action( icon, "&Menu action..." );

I would be interested in learning if this actually does work. I've never gotten around to testing it, but it seems like exactly the use this was designed for.

Caleb Huitt - cjhuitt
This is the correct way to do this, although I'd advise against using two radically different icons for the same action - it tends to confuse the user interface.
Thomi