Set inactive icon for action shown as a button in a toolbar
-
There are no errors. The code boils down to the following:
@action->setIcon(QIcon(":/icons/Create-Active.svg"));
action->icon().addPixmap(QPixmap(":/icons/Create-Inactive.svg"), QIcon::Disabled);@The first line works as expected, the action shows the corresponding image (Create-Active.svg). The second line apparently does nothing, because the disabled action shows grayscale version of Create-Active.svg, not Create-Inactive.svg.
-
you should rather use something like:
@
QIcon myIcon(":/icons/Create-Active.svg");
myIcon.addPixmap(QPixmap(":/icons/Create-Inactive.svg"), QIcon::Disabled);
action->setIcon(myIcon);
@ -
Interesting. It works correctly in Windows, but not on Android (the code is the same).
-
[quote author="SGaist" date="1424133957"]you should rather use something like:
@
QIcon myIcon(":/icons/Create-Active.svg");
myIcon.addPixmap(QPixmap(":/icons/Create-Inactive.svg"), QIcon::Disabled);
action->setIcon(myIcon);
@[/quote]Just to explain why: the action->icon() call returns a copy of the QIcon instance set on the action. Then you modify the copy, and throw that modified copy away. The icon set on the action is not modified in the process.
-
Thanks for the explanation. It is interesting why this code does not work on Android...
-
I have tested the code under Windows, Linux, Mac OS X and Android. It works everywhere except Android.
-
Maybe a silly question but does Android use the disabled state ?
-
I do not know. The situation with icons for disabled QAction states is strange. I have just found that setting icon for the disabled state in Qt Designer does not work (both in Windows and Linux). Setting the icon through the code posted above (used in the window's constructor) works as expected.
-
It gets stranger and stranger. I have a QAction with default icon (Create-Active.svg image). When I set an icon for the disabled state (actually it is QIcon::Mode, value QIcon::Disabled, state is QIcon::Off) in Qt Designer (Create-Inactive.svg image), it is correctly shown in the property editor on the right. However, in the pop-up dialog shown when I double-click the action in the list of actions the same icon is shown in grayscale (grayscale version of Create-Inactive.svg image). When I run the program, again I see the grayscale version of the disabled icon. If I add pixmap thorough the following code in window's constructor
@QIcon icon(":/icons/Create-Active.svg");
icon.addPixmap(QPixmap(":/icons/Create-Inactive.svg"), QIcon::Disabled);
action->setIcon(icon);@it works properly (I see Create-Inactive.svg image, not its grayscale version). It is the same under Windows and Linux.
Under Android it is even stranger. If I set the icon in the designer, the behavior is the same as under Windows or Linux (grayscale version of Create-Inactive.svg). So Android does use the disabled mode somehow. But, if I set Create-Inactive.svg icon in the code (with or without previously setting it in the designer), I see grayscale version of the default icon (Create-Active.svg). So setting it in the code somehow resets setting in the designer.
-
After looking through QIcon class sources I have found that the problem is related to icon size. If I render SVG image to pixmap of the correct size and then add it, it works under Android too:
@icon = QIcon(":/icons/Create-Active.svg");
QImage image = size, QImage::Format_ARGB32_Premultiplied);
image.fill(Qt::transparent);
QSvgRenderer r(QString(":/icons/Create-Inactive.svg"));
QPainter p(&image);
r.render(&p);
icon.addPixmap(QPixmap::fromImage(image), QIcon::Disabled);
action->setIcon(icon);@