I am using QWidget and its paintEvent.
Im using QPainter::scale(zoom,zoom) to scale background images on the widget.
Then I want to paint some foreground icons and keep their size the same all the time while preserving quality.
The problem is that even though the QPixamp is kept the same size, its quality decrades.
The bigger the “zoom” the bigger the loss in quality.
Minimal code example:
void myPainter::paintEvent(QPaintEvent *)
{
QPainter p(this);
p.scale(myZoom,myZoom)
.
.
.
double tagSize = t->icon_->height() / zoom;
QPoint tagpoint = rect().center();
p.drawPixmap(tagpoint, t->icon_->scaledToHeight(tagSize,Qt::SmoothTransformation));
}
I Used drawPixamp with scaled QPixmap.
I expected for the QPixamp quality to not decrade but stay the same.
EDIT:(a partial solution)
To keep the pixmap quality it was possible to
unzoom,draw,zoom to keep the image quality good and image samesize without resizing.
p.scale(1/zoom,1/zoom);
p.drawPixmap(tagpoint * zoom, *t->icon_);
p.scale(zoom,zoom);
This forced new calculation to the tag point the keep the position same, but it seems to work after calculating the position and taking the zoom to account in there.
0
The quality “degrades” because you’re scaling the pixmap down.
It’s like scaling a 100×100 image to 50×50 and then zooming it by a 2x ratio: the degradation is obvious because by scaling the image you lose the original contents, especially when scaling down.
Remember that a pixmap is a map of pixels (pix-map). Every scaling will always potentially result in a loss of contents, both when zooming in and out, with the exception of scaling up with integer factors and no scaling filter is applied.
Consider this canonical test image as a source, at 512×512:
Then scale it down by half the size:
Then this is how it would be painted with a “zoom in” factor of 2:
The “degradation” is caused by the fact that the transform applied on the painter can only work with the data it is provided: in this specific case, the reduced map of pixels of the scaled down image. You cannot get the “non-degraded” image, because that data does not exist in the scaled pixmap.
The only safe way to achieve an appropriate result without quality loss, then, is not by scaling the image, but to always draw the source with the appropriate transformation.
You can do it as you suggested in your question, or eventually by using the QPainter states.
p.save();
p.resetTransform();
// or, alternatively, p.scale(1/zoom, 1/zoom) as above
p.drawPixmap(tagpoint * zoom, *t->icon_);
p.restore();
The result would be identical to your proposal.
Note that saving the painter state may be slightly less efficient than just “unscale+paint+rescale” (as it saves its full state, including the current pen, brush, etc., which are obviously unnecessary in this specific case), but in some situations it is more effective, as having stacks of QPainter states may simplify code writing/readability/debugging.