From the documentation of QPixmap you can learn:
The hasAlpha(), setMask() and mask() functions are legacy and should not be used. They are potentially very slow.
Apart from being slow, they operate on a binary mask (QBitmap
) which does not support anti-aliasing, each pixel is either fully opaque or fully transparent. This results in jagged edges.
The solution is to manipulate the alpha channel of the pixmap directly. However, you cannot use drawing operations on a pixmap. Instead, you need to draw on the QImage before converting it via QPixmap::fromImage()
.
With this method, the alpha channel you manipulate has 8 bits (instead of 1) which allows antialiasing. At the edges you will find a smooth transition between fully opaque and fully transparent.
So to draw the alpha in the original QImage:
- Make sure that it actually has an alpha channel, e.g. by calling
img.convertToFormat(QImage::Format_ARGB32);
- Initialize your
QPainter
onimg
as paint device - Set the
DestinationIn
composition mode on the painter; see http://qt-project.org/doc/qt-4.8/qpainter.html#CompositionMode-enum - Perform the
drawEllipse
operation with a white brush of a certain alpha.