Antialiasing not working in QGraphicsView
Asked Answered
H

2

10

I re-implemented QGraphicsView to have the scene zoomed with a mouse wheel event. The scene contains several QGraphicsPixmapItem. The wheel event calls QGraphicsView::scale(qreal sx, qreal sy)

Everything works perfectly but the rendering. As I zoom out (the scene gets smaller), aliasing appears. I tried setting the render hints as following in the re-implemented QGraphicsView constructor:

ImageViewer::ImageViewer(QWidget * parent) :
  QGraphicsView(parent)
{
   setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::HighQualityAntialiasing);
}

I still see these artifacts. How can I get rid of this ?

Hercegovina answered 18/7, 2013 at 21:31 Comment(2)
Can you post a screenshot of what's going on? Are you setting any caching strategy on the items?Wendolynwendt
Thank you @Wendolynwendt ! I'm not setting anything about caching, so I guess it's default. Here's an example of what it looks like before (1:1 pixel ratio) and after (less than 1:1 pixel ratio) zooming out. You'll see some aliasing in the concentric circles.Hercegovina
G
10

Please see my comments for this question.

Basically you have to call setTransformationMode(Qt::SmoothTransformation) on the QGraphicsPixmapItems you want anti-aliasing to apply to.

Calling setRenderHints on the view did not work for me, either.

Gyrostatic answered 18/8, 2013 at 20:39 Comment(4)
So there's no way to have an antialiased background? Edit: Nm was an error in my code.Hegira
Using SmoothTransformation does some antialiasing when using fitInView, but it's basically next to nothing. I now scale the pixmap manually and set Qt.SmoothTransformation. This unfortunately wastes lots of time, but the result is much better.Hypnotize
I get an error saying "no member named 'setTransformationMode' in 'QGraphicsItem'".Octavo
@DonaldDuck It is only for QGraphicsPixmapItem. Answer updated.Gyrostatic
L
1

The render hints are only applied if it is set bevor the painter is used. Here a snipped:

QGraphicsPixmapItem *
drawGraphicsPixmapItem(const QRectF &rect)
{
    auto pixmap = new QPixmap(rect.size().toSize());
    pixmap->fill("lightGrey");

    auto painter = new QPainter(pixmap);
    // set render hints bevor drawing with painter
    painter->setRenderHints(QPainter::Antialiasing);

    QPen pen;
    pen.setColor("black");
    pen.setWidth(3);
    painter->setPen(pen);
    QRectF rectT = rect;
    rectT.adjust(pen.widthF()/2,pen.widthF()/2,-pen.widthF()/2,-pen.widthF()/2);

    QPainterPath circlePath;
    circlePath.addEllipse(rectT);
    circlePath.closeSubpath();
    painter->fillPath(circlePath,QBrush("green"));
    painter->drawPath(circlePath);
    auto pixmapItem = new QGraphicsPixmapItem(*pixmap);
    pixmapItem->setCacheMode(
             QGraphicsItem::CacheMode::DeviceCoordinateCache,
             pixmap->size() );
    return pixmapItem;
}
Lawrencelawrencium answered 16/7, 2018 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.