IMAN4K
IMAN4K

Reputation: 1345

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

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

Upvotes: 6

Views: 10514

Answers (1)

dtech
dtech

Reputation: 49279

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.

Upvotes: 13

Related Questions