Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: How to Change Icon Color #243

Open
mgh2010 opened this issue Sep 12, 2023 · 2 comments
Open

Question: How to Change Icon Color #243

mgh2010 opened this issue Sep 12, 2023 · 2 comments

Comments

@mgh2010
Copy link

mgh2010 commented Sep 12, 2023

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?

@dalthviz
Copy link
Member

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!

@sdbbs
Copy link

sdbbs commented Apr 16, 2024

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants