QPainter::drawPixmap() doesn't look good and has low quality?

妖精的绣舞 提交于 2019-12-21 02:30:28

问题


I'm trying to draw an icon(.png) inside a QWidget with QPainter::drawPixmap() :

QPixmap _source = "/.../.png";
painter.setRenderHint(QPainter::HighQualityAntialiasing);
painter.drawPixmap(rect(), _source);

but in comparing to QLabel (for example) and in lower size (19*19 in my case) the result isn't perfect.

What can I do?

****Edit****

QLabel with pixmap @ size 19*19:

My painting @ size 19*19 via SmoothPixmapTransform render type:


回答1:


You are setting the wrong render hint, you need QPainter::SmoothPixmapTransform to get smooth resizing. By default the nearest neighbor method is used, which is fast but has very low quality and pixelates the result.

QPainter::HighQualityAntialiasing is for when drawing lines and filling paths and such, i.e. when rasterizing geometry, it has no effect on drawing raster graphics.

EDIT: It seems there is only so much SmoothPixmapTransform can do, and when the end result is so tiny, it isn't much:

  QPainter p(this);
  QPixmap img("e://img.png");
  p.drawPixmap(QRect(50, 0, 50, 50), img);
  p.setRenderHint(QPainter::SmoothPixmapTransform);
  p.drawPixmap(QRect(0, 0, 50, 50), img);
  img = img.scaled(50, 50, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  p.drawPixmap(100, 0, img);

This code produces the following result:

There is barely any difference between the second and third image, manually scaling the source image to the desired dimensions and drawing it produces the best result. This is certainly not right, it is expected from SmoothTransformation to produce the same result, but for some reason its scaling is inferior to the scale() method of QPixmap.



来源:https://stackoverflow.com/questions/36894246/qpainterdrawpixmap-doesnt-look-good-and-has-low-quality

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