Set inactive icon for action shown as a button in a toolbar
-
wrote on 11 Feb 2015, 06:41 last edited by
I have an application with a toolbar populated with QAction objects. I can set the default (active) icons with QAction::setIcon(QIcon(path_to_icon_file)) method. I also would like to set the deisbled ("greyed out") icon for the actions. Theres is one "discussion I have found":http://stackoverflow.com/questions/1358563/set-a-custom-icon-for-a-qaction-when-disabled, but the approach described there does not work. The code with QAction::icon().addPixmap(QPixmap(path_to_icon_file), QIcon::Disabled) calls compiles and runs without errors, but the new icons are not displayed (instead of them there are grayscale versions of the default icons, as usual). Is it possible to set custom image for a disabled icon in QToolBar?
-
Hi,
What exact error are you getting ?
-
wrote on 16 Feb 2015, 13:17 last edited by
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);
@ -
wrote on 17 Feb 2015, 09:30 last edited by
Interesting. It works correctly in Windows, but not on Android (the code is the same).
-
wrote on 17 Feb 2015, 11:32 last edited by
[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.
-
wrote on 17 Feb 2015, 11:44 last edited by
Thanks for the explanation. It is interesting why this code does not work on Android...
-
wrote on 17 Feb 2015, 12:24 last edited by
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 ?
-
wrote on 18 Feb 2015, 09:40 last edited by
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.
-
wrote on 19 Feb 2015, 07:25 last edited by
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.
-
wrote on 19 Feb 2015, 14:38 last edited by
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);@
10/12