QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
painter.setPen(QPen(Qt::black, 3, Qt::DashDotLine, Qt::RoundCap));
painter.setBrush(QBrush(Qt::green, Qt::Dense1Pattern));
painter.drawEllipse(50, 200, 150, 80);
在绘制图形时,会出现锯齿的走样图形,Qt内置了反走样算法,默认是关闭的,需要显性的打开。
使用setRenderHint函数可以启动反走样,告诉QPainter用不同的颜色强度绘制边框以减少视觉扭曲,这种扭曲一般在边框转换为像素的时候发生。由此生成的结果是可以在支持这一特性的平台和设备上得到平滑的边缘。
反走样的效果要比走样的好得多。但是反走样是一种比较复杂的算法,在一些对图像质量要求不高的应用中,是不需要进行开启反走样的。
为了提高效率,一般的图形绘制,都是默认不开启反走样的。
QPainter::Antialiasing | 0x01 | Indicates that the engine should antialias edges of primitives if possible. |
QPainter::TextAntialiasing | 0x02 | Indicates that the engine should antialias text if possible. To forcibly disable antialiasing for text, do not use this hint. Instead, set QFont::NoAntialias on your font's style strategy. |
QPainter::SmoothPixmapTransform | 0x04 | Indicates that the engine should use a smooth pixmap transformation algorithm (such as bilinear) rather than nearest neighbor. |
QPainter::HighQualityAntialiasing | 0x08 | This value is obsolete and will be ignored, use the Antialiasing render hint instead. |
QPainter::NonCosmeticDefaultPen | 0x10 | This value is obsolete, the default for QPen is now non-cosmetic. |
QPainter::Qt4CompatiblePainting | 0x20 | Compatibility hint telling the engine to use the same X11 based fill rules as in Qt 4, where aliased rendering is offset by slightly less than half a pixel. Also will treat default constructed pens as cosmetic. Potentially useful when porting a Qt 4 application to Qt 5. |
QPainter::LosslessImageRendering | 0x40 | Use a lossless image rendering, whenever possible. Currently, this hint is only used when QPainter is employed to output a PDF file through QPrinter or QPdfWriter, where drawImage()/drawPixmap() calls will encode images using a lossless compression algorithm instead of lossy JPEG compression. This value was added in Qt 5.13. |