Qt World Summit: Submit your Presentation

Icon foreground color modification

  • I have an icon in .png format that is essentially a black symbol with a clear background. I'd like to apply this icon to a QPushButton, but with the color of the black foreground image to be set dynamically (not always black). Is there a good way to do this? I could produce multiple .png files with different colors, but I'd like the dynamic color range of the icon to be more "continuous" if possible.

    Thanks in advance!

  • You could try to turn the black pixels into transparent ones and set a background color on the button. This has the drawback that the complete button has that background color. There's no easy built-in way in Qt to change all pixels of a certain color to another color.

  • That would unfortunately result in the entire image being transparent.

    If not the ability to change the color of all pixels of a certain color, how about the ability to set the color of all opaque pixels? This would achieve the same result in this case since all opaque pixels are of the same color.


  • Well, you could load the image into a QImage, and loop over the pixels. You can change them one-by-one that way. If the shape is not very complicated, it would perhaps be better to just draw it yourself in the right color in Qt? Or, perhaps you can use Qt SVG capabilities and use that to render the shape in the right color?

Log in to reply