Qt : Smooth a circular mask. Remove Jagged edges

独自空忆成欢 提交于 2019-11-27 07:18:13

问题


I am currently doing the following for putting a circular mask on my image. As a result only circular region of my image is displayed.This works fine however the image tends to have jagged edges (border). Any suggestion on how I could do this.


回答1:


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.


来源:https://stackoverflow.com/questions/22335247/qt-smooth-a-circular-mask-remove-jagged-edges

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!