Antialiasing for QPixmap scaled in a QGraphicsScene
-
Ok guys, the question is simple: is it possible to obtain a smooth transformation when calling scale() on a QGraphicsScene containing QPixmaps. (Jake007 says yes, bu7ch3r says no)
If yes how?
If no, Jake007: you suggests to write my own AA, does it mean subclassing QGraphicsItem and reimplementing the paintEvent? -
Hi,
i encountered a similar behavior when scaling QGraphicsPixmapItems in an QGraphicsScene. As i would expect a scene Scale to promote to the same routines as an PixmapItem scale, the following solution might help you too:I set SmoothTransformation per PixmapItem (default is Qt::FastTransformation) and not only on the QGraphicsView.
itemPixmap->setScale(xx);
itemPixmap->setTransformationMode(Qt::SmoothTransformation); -
Hey, did you register just to answer me? Thanks a lot!
Calling setTransformationMode(Qt::SmoothTransformation); on the QGraphicsPixmapItem worked. Now the pixmap is blurred when I zoom in.
But I still have a strong moiré effect when I zoom out.I tried to set this on the QGraphicsView, but it does not help:
@ setRenderHint(QPainter::HighQualityAntialiasing, true);
setRenderHint(QPainter::SmoothPixmapTransform, true);@ -
wow, i'm a newbie and i have the same question now.
-
I am having exactly the same problem. Here is a short summary what my problem is and what i've tried so far:
I have a Qgraphicsview with qgraphicspixmapitems in it. Now if i scale the view (zoom out), i get those ugly moire effects:
!http://i.imgur.com/Eu0S7a2.jpg(Moire effect)!
what i want though is this, scaled in photoshop:
!http://i.imgur.com/mwRiq03.jpg(no moire)!
I tried subclassing qgraphicspixmapitem and set the render hints:
@void xGraphicsPreviewItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
painter->setRenderHint( QPainter::Antialiasing, true);
painter->setRenderHint( QPainter::HighQualityAntialiasing, true);
QGraphicsPixmapItem::paint(painter, option, widget);
}@and also this:
@void xGraphicsPreviewItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
painter->setRenderHint( QPainter::Antialiasing, true);
painter->setRenderHint( QPainter::HighQualityAntialiasing, true);
painter->drawPixmap(boundingRect().topLeft(), this->pixmap());
}@But with both the renderhints do not have any effect on the images. Any help/advice?
Thanks and Regards
-
Hi Jassie,
I had to implement it completely manually.
Simply create small resized versions of your image when loading it or on demand and load them into your graphicsview depending on the zoom value.You get the best results when your image has the actual size in which it is displayed in. (e.g. if you zoom out so your image will only be 512px wide, resize it to 512px before loading it into the interface)
-
Hi Jassie,
I had to implement it completely manually.
Simply create small resized versions of your image when loading it or on demand and load them into your graphicsview depending on the zoom value.You get the best results when your image has the actual size in which it is displayed in. (e.g. if you zoom out so your image will only be 512px wide, resize it to 512px before loading it into the interface)
-
As an example:
I load my original image with size 1024px. It is displayed in my graphicsview with the same size.
Now i zoom out 50%. But instead of scaling the graphicsview, simply resize the original image to 512px (with QT or openCV) and reload it into the graphicsview. -
As an example:
I load my original image with size 1024px. It is displayed in my graphicsview with the same size.
Now i zoom out 50%. But instead of scaling the graphicsview, simply resize the original image to 512px (with QT or openCV) and reload it into the graphicsview. -
When you want the best result there is no other option as drawing the image in exact the size shown on screen.(in Pixel)
To archive this you could subclass QGraphicsPixmapItem and override paint.
There you have to load the pixmap in the correct size (try to map your scene coordinates to pixels).
To load an Image in an scaled size you could for example use QImageReader :
http://doc.qt.io/qt-5/qimagereader.html#setScaledSize
Of course you have to cache the loaded Pixmap yourself.
When you reach paint event with the same size your code should use the cached pixmap. -
When you want the best result there is no other option as drawing the image in exact the size shown on screen.(in Pixel)
To archive this you could subclass QGraphicsPixmapItem and override paint.
There you have to load the pixmap in the correct size (try to map your scene coordinates to pixels).
To load an Image in an scaled size you could for example use QImageReader :
http://doc.qt.io/qt-5/qimagereader.html#setScaledSize
Of course you have to cache the loaded Pixmap yourself.
When you reach paint event with the same size your code should use the cached pixmap. -
Or simply calculate all possible images for all zoom steps at the start.
That means if your zoom values range from 10% to 100% with zoom step +10% simply calculate all 9 images at the start, cache them and load them depending on the zoom value.
QPixmap pixmap100, pixmap90, pixmap80, pixmap70 ... (etc)
you would have to limit the number of possible zoom values of course to avoid long loading times and huge amount of data.