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

1

6

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:

enter image description here

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

enter image description here

Kweichow answered 27/4, 2016 at 15:26 Comment(0)
B
13

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:

enter image description here

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.

Blunder answered 27/4, 2016 at 15:34 Comment(2)
Thanks.i tried what you said but the result is the same!Kweichow
@Kweichow how big is the original image?Blunder

© 2022 - 2024 — McMap. All rights reserved.