When drawing images with Qt, the sequence of calling the drawImage() function will determine which image is being rendered first and which one is rendered later. This will affect the depth order of the images and yield different outcomes.
In the previous example, we called drawImage() function four times to draw four different images onscreen. The first drawImage() function renders checker.png, and the second drawImage() function renders tux.png (the penguin). The image that gets rendered later will always appear in front of the others, which is why the penguin is showing in front of the checker pattern. The same goes for the butterfly and the checker on the right. The reason why you can still see the checker even though the butterfly is rendered in front of it is because the butterfly image is not fully opaque.
Now, let's invert the render sequence and see what happens. We will try to render the penguin first, followed by the checker box. The same goes for the other pair of images on the right: the butterfly gets rendered first, followed by the checker box:
To apply a composition effect to the image, we'll have to set the painter's composition mode before drawing the image, by calling the painter.setCompositionMode() function. You can pick a desired composition mode from the auto-complete menu by typing QPainter::CompositionMode.
In the previous example, we applied QPainter::CompositionMode_Difference to the checker box on the left, which inverted its color. Next, we applied QPainter::CompositionMode_Overlay to the penguin, which makes it blend with the checker, and were able to see both images overlaying each other. On the right-hand side, we applied QPainter::CompositionMode_Xor to the checker, where if differences exist between the source and destination, colors are shown; otherwise, it will be rendered black. Since it's comparing differences with the white background, the non-transparent part of the checker becomes completely black. We also applied QPainter::CompositionMode_SoftLight to the butterfly image. This blends the pixels with the background with reduced contrast. If you want to disable the composition mode that you've just set for the previous rendering before proceeding to the next, simply set it back to the default mode, which is QPainter::CompositionMode_SourceOver.