Qt : Smooth a circular mask. Remove Jagged edges

情到浓时终转凉″ 提交于 2019-11-28 12:55:16

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:

  1. Make sure that it actually has an alpha channel, e.g. by calling img.convertToFormat(QImage::Format_ARGB32);
  2. Initialize your QPainter on img as paint device
  3. Set the DestinationIn composition mode on the painter; see http://qt-project.org/doc/qt-4.8/qpainter.html#CompositionMode-enum
  4. Perform the drawEllipse operation with a white brush of a certain alpha.
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!