qtawesome
qtawesome copied to clipboard
Question: How to Change Icon Color
i have Question: there is in some situation that we want to change the color for the icon. Is there an option to change the color or do we have to delete the icon and create it again with a new color?
Hi @mgh2010 ! Not totally sure what would be triggering the color change in your use case but maybe a check to the toggle state styling example could be useful: https://qtawesome.readthedocs.io/en/latest/usage.html#examples (Apply toggling state styling
item).
Let us know if the info above helps!
I'm not sure if my question is same as OP, but worth a try, I guess.
I am aware, that I can get a QIcon with colored glyph when it is instantiated like this:
self.qicon = qta.icon('fa5s.spinner', color='darkgray', animation=self.animation)
Then, I can use this to set pixmap of a QMessageBox, which works fine, too:
self.msgBox.setIconPixmap(self.qicon.pixmap(self.icon_qsize))
However, then the icon is the same color, from instantiation to end of its lifetime.
My question is: Sure we can change color, if we instantiate a new QIcon with e.g. red color after 10 seconds (and delete the previus qicon from memory) -- but can we change color of the same QIcon after instantiation (without deleting it and instantiating a new open)? Say, I show the icon 10 seconds in darkgray, then 10 seconds in red (without instantiating a new QIcon)? ... I guess that is what OP meant by:
Is there an option to change the color or do we have to delete the icon and create it again with a new color?
This could also help with animating the icon color, I guess ...
In any case, I can see that IconicFont basically uses draw color from a dict; one can do qta.set_defaults(color='green')
, which calls iconic_font.set_global_defaults(color='green')
, which sets _default_options['color'] = 'green'
- but then, this is only transferred to options
in IconicFont.parse_options
via live_dict
; and then this becomes api_options
, and it is api_options
used for drawing the icon via self.icon_cache[cache_key] = self._icon_by_painter(self.painter, api_options)
... In other words, in theory, if I could do:
self.qicon = qta.icon(inchoice, color='darkgray', animation=self.animation)
qta.set_api_options(color='green') # pseudo!
self.msgBox.setIconPixmap(self.qicon.pixmap(self.icon_qsize))
... I could (maybe) hope the pixmap applied to messagebox to be green; however, there is no equivalent to set_api_options
in the API I could find - and if I just do set_defaults
instead:
self.qicon = qta.icon(inchoice, color='darkgray', animation=self.animation)
qta.set_defaults(color='green')
self.msgBox.setIconPixmap(self.qicon.pixmap(self.icon_qsize))
... then this has no effect, - somewhat as expected, because by the time I've said set_default
, the qta.icon has already applied the api_options
, including the color that is going to be used to render the glyph.
So, yeah - is there a way to change the icon glyph color, without re-instantiating the qicon for that glyph?
( my problem is that I have an init method that already accepts 10003204958209384092834098 arguments, and I have a headache just looking at it, and I really do not want to add a new input argument "glyph_color" to this method, I'd rather just do the instantiation first, and change color later)