Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 847
IMAN4K Avatar asked Apr 27 '16 15:04

IMAN4K


1 Answers

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.

like image 191
dtech Avatar answered Sep 21 '22 23:09

dtech