Congratulations to our 2022 Qt Champions!

How to alpha-blend a color-inverted image on top of another image with QPainter?

  • I am drawing an image that consists of two logical "layers" (terrain & objects). When a special mode is active, I want to draw the second layer (objects) with inverted color. Individual objects are translucent and overlapping, so I am first drawing them all with normal alpha-blending (CompositionMode_SourceOver) into an offscreen pixmap. Then I need to invert the color of the result (without touching alpha!) and blend it on top of the destination (terrain).

    I tried using RasterOp_NotSource. Obviously, it doesn't include alpha-blending, so I tried to do it in 2 steps as a workaround (with another temporary pixmap in between). Unfortunately, for some reason RasterOp_NotSource destroys my alpha by setting it all to 0xFF regardless of what it was, so that doesn't work either.

    So how can I achieve what I need?

  • Update: I've found QImage::invertPixels() which seems to do what I want (and the result can then be blended as normally). Not sure yet if this is the best solution in terms of performance, but at least it works. :)

Log in to reply